VirtualBox

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

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

duh.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.9 KB
Line 
1/* $Id: dbgmoddwarf.cpp 38585 2011-08-31 14:45:06Z 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/log.h>
39#include <iprt/mem.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42#include "internal/dbgmod.h"
43
44
45/*******************************************************************************
46* Defined Constants And Macros *
47*******************************************************************************/
48/** @name Standard DWARF Line Number Opcodes
49 * @{ */
50#define DW_LNS_extended UINT8_C(0)
51#define DW_LNS_copy UINT8_C(1)
52#define DW_LNS_advance_pc UINT8_C(2)
53#define DW_LNS_advance_line UINT8_C(3)
54#define DW_LNS_set_file UINT8_C(4)
55#define DW_LNS_set_column UINT8_C(5)
56#define DW_LNS_negate_stmt UINT8_C(6)
57#define DW_LNS_set_basic_block UINT8_C(7)
58#define DW_LNS_const_add_pc UINT8_C(8)
59#define DW_LNS_fixed_advance_pc UINT8_C(9)
60#define DW_LNS_set_prologue_end UINT8_C(10)
61#define DW_LNS_set_epilogue_begin UINT8_C(11)
62#define DW_LNS_set_isa UINT8_C(12)
63/** @} */
64
65
66/** @name Extended DWARF Line Number Opcodes
67 * @{ */
68#define DW_LNE_end_sequence UINT8_C(1)
69#define DW_LNE_set_address UINT8_C(2)
70#define DW_LNE_define_file UINT8_C(3)
71#define DW_LNE_set_descriminator UINT8_C(4)
72/** @} */
73
74
75/*******************************************************************************
76* Structures and Typedefs *
77*******************************************************************************/
78/**
79 * DWARF sections.
80 */
81typedef enum krtDbgModDwarfSect
82{
83 krtDbgModDwarfSect_abbrev = 0,
84 krtDbgModDwarfSect_aranges,
85 krtDbgModDwarfSect_frame,
86 krtDbgModDwarfSect_info,
87 krtDbgModDwarfSect_inlined,
88 krtDbgModDwarfSect_line,
89 krtDbgModDwarfSect_loc,
90 krtDbgModDwarfSect_macinfo,
91 krtDbgModDwarfSect_pubnames,
92 krtDbgModDwarfSect_pubtypes,
93 krtDbgModDwarfSect_ranges,
94 krtDbgModDwarfSect_str,
95 krtDbgModDwarfSect_types,
96 /** End of valid parts (exclusive). */
97 krtDbgModDwarfSect_End
98} krtDbgModDwarfSect;
99
100/**
101 * Abbreviation cache entry.
102 */
103typedef struct RTDBGMODDWARFABBREV
104{
105 /** Whether this entry is filled in or not. */
106 bool fFilled;
107 /** Whether there are children or not. */
108 bool fChildren;
109 /** The tag. */
110 uint16_t uTag;
111 /** Offset into the abbrev section of the specification pairs. */
112 uint32_t offSpec;
113} RTDBGMODDWARFABBREV;
114/** Pointer to an abbreviation cache entry. */
115typedef RTDBGMODDWARFABBREV *PRTDBGMODDWARFABBREV;
116/** Pointer to a const abbreviation cache entry. */
117typedef RTDBGMODDWARFABBREV const *PCRTDBGMODDWARFABBREV;
118
119
120/**
121 * The instance data of the DWARF reader.
122 */
123typedef struct RTDBGMODDWARF
124{
125 /** The debug container containing doing the real work. */
126 RTDBGMOD hCnt;
127 /** Pointer to back to the debug info module (no reference ofc). */
128 PRTDBGMODINT pMod;
129
130 /** DWARF debug info sections. */
131 struct
132 {
133 /** The file offset of the part. */
134 RTFOFF offFile;
135 /** The size of the part. */
136 size_t cb;
137 /** The memory mapping of the part. */
138 void const *pv;
139 /** Set if present. */
140 bool fPresent;
141 } aSections[krtDbgModDwarfSect_End];
142
143 /** The offset into the abbreviation section of the current cache. */
144 uint32_t offCachedAbbrev;
145 /** The number of cached abbreviations we've allocated space for. */
146 uint32_t cCachedAbbrevsAlloced;
147 /** Used for range checking cache lookups. */
148 uint32_t cCachedAbbrevs;
149 /** Array of cached abbreviations, indexed by code. */
150 PRTDBGMODDWARFABBREV paCachedAbbrevs;
151 /** Used by rtDwarfAbbrev_Lookup when the result is uncachable. */
152 RTDBGMODDWARFABBREV LookupAbbrev;
153} RTDBGMODDWARF;
154/** Pointer to instance data of the DWARF reader. */
155typedef RTDBGMODDWARF *PRTDBGMODDWARF;
156
157/**
158 * DWARF cursor for reading byte data.
159 */
160typedef struct RTDWARFSECTRDR
161{
162 /** The current position. */
163 uint8_t const *pb;
164 /** The number of bytes left to read. */
165 size_t cbLeft;
166 /** The number of bytes left to read in the current unit. */
167 size_t cbUnitLeft;
168 /** The DWARF debug info reader instance. */
169 PRTDBGMODDWARF pDwarfMod;
170 /** Set if this is 64-bit DWARF, clear if 32-bit. */
171 bool f64bitDwarf;
172 /** Set if the format endian is native, clear if endian needs to be
173 * inverted. */
174 bool fNativEndian;
175 /** The size of a native address. */
176 uint8_t cbNativeAddr;
177 /** The cursor status code. This is VINF_SUCCESS until some error
178 * occurs. */
179 int rc;
180 /** The start of the area covered by the cursor.
181 * Used for repositioning the cursor relative to the start of a section. */
182 uint8_t const *pbStart;
183 /** The section. */
184 krtDbgModDwarfSect enmSect;
185} RTDWARFCURSOR;
186/** Pointer to a DWARF section reader. */
187typedef RTDWARFCURSOR *PRTDWARFCURSOR;
188
189
190/**
191 * DWARF line number program state.
192 */
193typedef struct RTDWARFLINESTATE
194{
195 /** Virtual Line Number Machine Registers. */
196 struct
197 {
198 uint64_t uAddress;
199 uint64_t idxOp;
200 uint32_t iFile;
201 uint32_t uLine;
202 uint32_t uColumn;
203 bool fIsStatement;
204 bool fBasicBlock;
205 bool fEndSequence;
206 bool fPrologueEnd;
207 bool fEpilogueBegin;
208 uint32_t uIsa;
209 uint32_t uDiscriminator;
210 } Regs;
211 /** @} */
212
213 /** Header. */
214 struct
215 {
216 uint32_t uVer;
217 uint64_t offFirstOpcode;
218 uint8_t cbMinInstr;
219 uint8_t cMaxOpsPerInstr;
220 uint8_t u8DefIsStmt;
221 int8_t s8LineBase;
222 uint8_t u8LineRange;
223 uint8_t u8OpcodeBase;
224 uint8_t const *pacStdOperands;
225 } Hdr;
226
227 /** @name Include Path Table (0-based)
228 * @{ */
229 const char **papszIncPaths;
230 uint32_t cIncPaths;
231 /** @} */
232
233 /** @name File Name Table (0-based, dummy zero entry)
234 * @{ */
235 char **papszFileNames;
236 uint32_t cFileNames;
237 /** @} */
238
239 /** The DWARF debug info reader instance. */
240 PRTDBGMODDWARF pDwarfMod;
241} RTDWARFLINESTATE;
242/** Pointer to a DWARF line number program state. */
243typedef RTDWARFLINESTATE *PRTDWARFLINESTATE;
244
245
246/** @callback_method_impl{FNRTLDRENUMSEGS} */
247static DECLCALLBACK(int) rtDbgModHlpAddSegmentCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
248{
249 PRTDBGMODINT pMod = (PRTDBGMODINT)pvUser;
250 Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
251 pSeg->cchName, pSeg->pchName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
252 RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
253#if 1
254 return pMod->pDbgVt->pfnSegmentAdd(pMod, pSeg->RVA, cb, pSeg->pchName, pSeg->cchName, 0 /*fFlags*/, NULL);
255#else
256 return pMod->pDbgVt->pfnSegmentAdd(pMod, pSeg->LinkAddress, cb, pSeg->pchName, pSeg->cchName, 0 /*fFlags*/, NULL);
257#endif
258}
259
260
261/**
262 * Calls pfnSegmentAdd for each segment in the executable image.
263 *
264 * @returns IPRT status code.
265 * @param pMod The debug module.
266 */
267DECLHIDDEN(int) rtDbgModHlpAddSegmentsFromImage(PRTDBGMODINT pMod)
268{
269 AssertReturn(pMod->pImgVt, VERR_INTERNAL_ERROR_2);
270 return pMod->pImgVt->pfnEnumSegments(pMod, rtDbgModHlpAddSegmentCallback, pMod);
271}
272
273
274
275
276/**
277 * Loads a DWARF section from the image file.
278 *
279 * @returns IPRT status code.
280 * @param pThis The DWARF instance.
281 * @param enmSect The section to load.
282 */
283static int rtDbgModDwarfLoadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
284{
285 /*
286 * Don't load stuff twice.
287 */
288 if (pThis->aSections[enmSect].pv)
289 return VINF_SUCCESS;
290
291 /*
292 * Sections that are not present cannot be loaded, treat them like they
293 * are empty
294 */
295 if (!pThis->aSections[enmSect].fPresent)
296 {
297 Assert(pThis->aSections[enmSect].cb);
298 return VINF_SUCCESS;
299 }
300 if (!pThis->aSections[enmSect].cb)
301 return VINF_SUCCESS;
302
303 /*
304 * Sections must be readable with the current image interface.
305 */
306 if (pThis->aSections[enmSect].offFile < 0)
307 return VERR_OUT_OF_RANGE;
308
309 /*
310 * Do the job.
311 */
312 return pThis->pMod->pImgVt->pfnMapPart(pThis->pMod, pThis->aSections[enmSect].offFile, pThis->aSections[enmSect].cb,
313 &pThis->aSections[enmSect].pv);
314}
315
316
317/**
318 * Unloads a DWARF section previously mapped by rtDbgModDwarfLoadSection.
319 *
320 * @returns IPRT status code.
321 * @param pThis The DWARF instance.
322 * @param enmSect The section to unload.
323 */
324static int rtDbgModDwarfUnloadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
325{
326 if (!pThis->aSections[enmSect].pv)
327 return VINF_SUCCESS;
328
329 int rc = pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[enmSect].cb, &pThis->aSections[enmSect].pv);
330 AssertRC(rc);
331 return rc;
332}
333
334
335/**
336 * Converts to UTF-8 or otherwise makes sure it's valid UTF-8.
337 *
338 * @returns IPRT status code.
339 * @param pThis The DWARF instance.
340 * @param ppsz Pointer to the string pointer. May be
341 * reallocated (RTStr*).
342 */
343static int rtDbgModDwarfStringToUtf8(PRTDBGMODDWARF pThis, char **ppsz)
344{
345 RTStrPurgeEncoding(*ppsz);
346 return VINF_SUCCESS;
347}
348
349
350/**
351 * Convers a link address into a segment+offset or RVA.
352 *
353 * @returns IPRT status code.
354 * @param pThis The DWARF instance.
355 * @param LinkAddress The address to convert..
356 * @param piSeg The segment index.
357 * @param poffSeg Where to return the segment offset.
358 */
359static int rtDbgModDwarfLinkAddressToSegOffset(PRTDBGMODDWARF pThis, uint64_t LinkAddress,
360 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
361{
362 return pThis->pMod->pImgVt->pfnLinkAddressToSegOffset(pThis->pMod, LinkAddress, piSeg, poffSeg);
363}
364
365
366/*
367 *
368 * DWARF Cursor.
369 * DWARF Cursor.
370 * DWARF Cursor.
371 *
372 */
373
374
375/**
376 * Reads a 8-bit unsigned integer and advances the cursor.
377 *
378 * @returns 8-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
379 * uErrValue is returned.
380 * @param pCursor The cursor.
381 * @param uErrValue What to return on read error.
382 */
383static uint8_t rtDwarfCursor_GetU8(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
384{
385 if (pCursor->cbUnitLeft < 1)
386 {
387 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
388 return uErrValue;
389 }
390
391 uint8_t u8 = pCursor->pb[0];
392 pCursor->pb += 1;
393 pCursor->cbUnitLeft -= 1;
394 pCursor->cbLeft -= 1;
395 return u8;
396}
397
398
399/**
400 * Reads a 16-bit unsigned integer and advances the cursor.
401 *
402 * @returns 16-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
403 * uErrValue is returned.
404 * @param pCursor The cursor.
405 * @param uErrValue What to return on read error.
406 */
407static uint16_t rtDwarfCursor_GetU16(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
408{
409 if (pCursor->cbUnitLeft < 2)
410 {
411 pCursor->pb += pCursor->cbUnitLeft;
412 pCursor->cbLeft -= pCursor->cbUnitLeft;
413 pCursor->cbUnitLeft = 0;
414 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
415 return uErrValue;
416 }
417
418 uint16_t u16 = RT_MAKE_U16(pCursor->pb[0], pCursor->pb[1]);
419 pCursor->pb += 2;
420 pCursor->cbUnitLeft -= 2;
421 pCursor->cbLeft -= 2;
422 if (!pCursor->fNativEndian)
423 u16 = RT_BSWAP_U16(u16);
424 return u16;
425}
426
427
428/**
429 * Reads a 32-bit unsigned integer and advances the cursor.
430 *
431 * @returns 32-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
432 * uErrValue is returned.
433 * @param pCursor The cursor.
434 * @param uErrValue What to return on read error.
435 */
436static uint32_t rtDwarfCursor_GetU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
437{
438 if (pCursor->cbUnitLeft < 4)
439 {
440 pCursor->pb += pCursor->cbUnitLeft;
441 pCursor->cbLeft -= pCursor->cbUnitLeft;
442 pCursor->cbUnitLeft = 0;
443 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
444 return uErrValue;
445 }
446
447 uint32_t u32 = RT_MAKE_U32_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3]);
448 pCursor->pb += 4;
449 pCursor->cbUnitLeft -= 4;
450 pCursor->cbLeft -= 4;
451 if (!pCursor->fNativEndian)
452 u32 = RT_BSWAP_U32(u32);
453 return u32;
454}
455
456
457/**
458 * Reads a 64-bit unsigned integer and advances the cursor.
459 *
460 * @returns 64-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
461 * uErrValue is returned.
462 * @param pCursor The cursor.
463 * @param uErrValue What to return on read error.
464 */
465static uint64_t rtDwarfCursor_GetU64(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
466{
467 if (pCursor->cbUnitLeft < 8)
468 {
469 pCursor->pb += pCursor->cbUnitLeft;
470 pCursor->cbLeft -= pCursor->cbUnitLeft;
471 pCursor->cbUnitLeft = 0;
472 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
473 return uErrValue;
474 }
475
476 uint64_t u64 = RT_MAKE_U64_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3],
477 pCursor->pb[4], pCursor->pb[5], pCursor->pb[6], pCursor->pb[7]);
478 pCursor->pb += 8;
479 pCursor->cbUnitLeft -= 8;
480 pCursor->cbLeft -= 8;
481 if (!pCursor->fNativEndian)
482 u64 = RT_BSWAP_U64(u64);
483 return u64;
484}
485
486
487/**
488 * Reads an unsigned LEB128 encoded number.
489 *
490 * @returns unsigned 64-bit number. On error RTDWARFCURSOR::rc is set and @a
491 * uErrValue is returned.
492 * @param pCursor The cursor.
493 * @param uErrValue The value to return on error.
494 */
495static uint64_t rtDwarfCursor_GetULeb128(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
496{
497 if (pCursor->cbUnitLeft < 1)
498 {
499 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
500 return uErrValue;
501 }
502
503 /*
504 * Special case - single byte.
505 */
506 uint8_t b = pCursor->pb[0];
507 if (!(b & 0x80))
508 {
509 pCursor->pb += 1;
510 pCursor->cbUnitLeft -= 1;
511 pCursor->cbLeft -= 1;
512 return b;
513 }
514
515 /*
516 * Generic case.
517 */
518 /* Decode. */
519 uint32_t off = 1;
520 uint64_t u64Ret = b & 0x7f;
521 do
522 {
523 if (off == pCursor->cbUnitLeft)
524 {
525 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
526 u64Ret = uErrValue;
527 break;
528 }
529 b = pCursor->pb[off];
530 u64Ret |= (b & 0x7f) << off * 7;
531 off++;
532 } while (b & 0x80);
533
534 /* Update the cursor. */
535 pCursor->pb += off;
536 pCursor->cbUnitLeft -= off;
537 pCursor->cbLeft -= off;
538
539 /* Check the range. */
540 uint32_t cBits = off * 7;
541 if (cBits > 64)
542 {
543 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
544 u64Ret = uErrValue;
545 }
546
547 return u64Ret;
548}
549
550
551/**
552 * Reads a signed LEB128 encoded number.
553 *
554 * @returns signed 64-bit number. On error RTDWARFCURSOR::rc is set and @a
555 * uErrValue is returned.
556 * @param pCursor The cursor.
557 * @param sErrValue The value to return on error.
558 */
559static int64_t rtDwarfCursor_GetSLeb128(PRTDWARFCURSOR pCursor, int64_t sErrValue)
560{
561 if (pCursor->cbUnitLeft < 1)
562 {
563 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
564 return sErrValue;
565 }
566
567 /*
568 * Special case - single byte.
569 */
570 uint8_t b = pCursor->pb[0];
571 if (!(b & 0x80))
572 {
573 pCursor->pb += 1;
574 pCursor->cbUnitLeft -= 1;
575 pCursor->cbLeft -= 1;
576 if (b & 0x40)
577 b |= 0x80;
578 return (int8_t)b;
579 }
580
581 /*
582 * Generic case.
583 */
584 /* Decode it. */
585 uint32_t off = 1;
586 uint64_t u64Ret = b & 0x7f;
587 do
588 {
589 if (off == pCursor->cbUnitLeft)
590 {
591 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
592 u64Ret = (uint64_t)sErrValue;
593 break;
594 }
595 b = pCursor->pb[off];
596 u64Ret |= (b & 0x7f) << off * 7;
597 off++;
598 } while (b & 0x80);
599
600 /* Update cursor. */
601 pCursor->pb += off;
602 pCursor->cbUnitLeft -= off;
603 pCursor->cbLeft -= off;
604
605 /* Check the range. */
606 uint32_t cBits = off * 7;
607 if (cBits > 64)
608 {
609 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
610 u64Ret = (uint64_t)sErrValue;
611 }
612 /* Sign extend the value. */
613 else if (u64Ret & RT_BIT_64(cBits - 1))
614 u64Ret |= ~(RT_BIT_64(cBits - 1) - 1);
615
616 return (int64_t)u64Ret;
617}
618
619
620/**
621 * Reads an unsigned LEB128 encoded number, max 32-bit width.
622 *
623 * @returns unsigned 32-bit number. On error RTDWARFCURSOR::rc is set and @a
624 * uErrValue is returned.
625 * @param pCursor The cursor.
626 * @param uErrValue The value to return on error.
627 */
628static uint32_t rtDwarfCursor_GetULeb128AsU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
629{
630 uint64_t u64 = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
631 if (u64 > UINT32_MAX)
632 {
633 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
634 return uErrValue;
635 }
636 return (uint32_t)u64;
637}
638
639
640/**
641 * Reads a signed LEB128 encoded number, max 32-bit width.
642 *
643 * @returns signed 32-bit number. On error RTDWARFCURSOR::rc is set and @a
644 * uErrValue is returned.
645 * @param pCursor The cursor.
646 * @param sErrValue The value to return on error.
647 */
648static int32_t rtDwarfCursor_GetSLeb128AsS32(PRTDWARFCURSOR pCursor, int32_t sErrValue)
649{
650 int64_t s64 = rtDwarfCursor_GetSLeb128(pCursor, sErrValue);
651 if (s64 > INT32_MAX || s64 < INT32_MIN)
652 {
653 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
654 return sErrValue;
655 }
656 return (int32_t)s64;
657}
658
659
660/**
661 * Skips a LEB128 encoded number.
662 *
663 * @returns IPRT status code.
664 * @param pCursor The cursor.
665 */
666static int rtDwarfCursor_SkipLeb128(PRTDWARFCURSOR pCursor)
667{
668 if (pCursor->cbUnitLeft < 1)
669 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
670
671 uint32_t offSkip = 1;
672 if (pCursor->pb[0] & 0x80)
673 do
674 {
675 if (offSkip == pCursor->cbUnitLeft)
676 {
677 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
678 break;
679 }
680 } while (pCursor->pb[offSkip++] & 0x80);
681
682 pCursor->pb += offSkip;
683 pCursor->cbUnitLeft -= offSkip;
684 pCursor->cbLeft -= offSkip;
685 return pCursor->rc;
686}
687
688
689/**
690 * Reads a zero terminated string, advancing the cursor beyond the terminator.
691 *
692 * @returns Pointer to the string.
693 * @param pCursor The cursor.
694 * @param pszErrValue What to return if the string isn't terminated
695 * before the end of the unit.
696 */
697static const char *rtDwarfCursor_GetSZ(PRTDWARFCURSOR pCursor, const char *pszErrValue)
698{
699 const char *pszRet = (const char *)pCursor->pb;
700 for (;;)
701 {
702 if (!pCursor->cbUnitLeft)
703 {
704 pCursor->rc = VERR_DWARF_BAD_STRING;
705 return pszErrValue;
706 }
707 pCursor->cbUnitLeft--;
708 pCursor->cbLeft--;
709 if (!*pCursor->pb++)
710 break;
711 }
712 return pszRet;
713}
714
715
716/**
717 * Reads an unsigned DWARF half number.
718 *
719 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
720 * uErrValue is returned.
721 * @param pCursor The cursor.
722 * @param uErrValue What to return on error.
723 */
724static uint16_t rtDwarfCursor_GetUHalf(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
725{
726 return rtDwarfCursor_GetU16(pCursor, uErrValue);
727}
728
729
730/**
731 * Reads an unsigned DWARF byte number.
732 *
733 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
734 * uErrValue is returned.
735 * @param pCursor The cursor.
736 * @param uErrValue What to return on error.
737 */
738static uint8_t rtDwarfCursor_GetUByte(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
739{
740 return rtDwarfCursor_GetU8(pCursor, uErrValue);
741}
742
743
744/**
745 * Reads a signed DWARF byte number.
746 *
747 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
748 * uErrValue is returned.
749 * @param pCursor The cursor.
750 * @param uErrValue What to return on error.
751 */
752static int8_t rtDwarfCursor_GetSByte(PRTDWARFCURSOR pCursor, int8_t iErrValue)
753{
754 return (int8_t)rtDwarfCursor_GetU8(pCursor, (uint8_t)iErrValue);
755}
756
757
758/**
759 * Reads a unsigned DWARF offset value.
760 *
761 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
762 * uErrValue is returned.
763 * @param pCursor The cursor.
764 * @param uErrValue What to return on error.
765 */
766static uint64_t rtDwarfCursor_GetUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
767{
768 if (pCursor->f64bitDwarf)
769 return rtDwarfCursor_GetU64(pCursor, uErrValue);
770 return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
771}
772
773
774/**
775 * Reads a unsigned DWARF native offset value.
776 *
777 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
778 * uErrValue is returned.
779 * @param pCursor The cursor.
780 * @param uErrValue What to return on error.
781 */
782static uint64_t rtDwarfCursor_GetNativeUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
783{
784 switch (pCursor->cbNativeAddr)
785 {
786 case 1: return rtDwarfCursor_GetU8(pCursor, (uint8_t )uErrValue);
787 case 2: return rtDwarfCursor_GetU16(pCursor, (uint16_t)uErrValue);
788 case 4: return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
789 case 8: return rtDwarfCursor_GetU64(pCursor, uErrValue);
790 default:
791 pCursor->rc = VERR_INTERNAL_ERROR_2;
792 return uErrValue;
793 }
794}
795
796
797/**
798 * Gets the unit length, updating the unit length member and DWARF bitness
799 * members of the cursor.
800 *
801 * @returns The unit length.
802 * @param pCursor The cursor.
803 */
804static uint64_t rtDwarfCursor_GetInitalLength(PRTDWARFCURSOR pCursor)
805{
806 /*
807 * Read the initial length.
808 */
809 pCursor->cbUnitLeft = pCursor->cbLeft;
810 uint64_t cbUnit = rtDwarfCursor_GetU32(pCursor, 0);
811 if (cbUnit != UINT32_C(0xffffffff))
812 pCursor->f64bitDwarf = false;
813 else
814 {
815 pCursor->f64bitDwarf = true;
816 cbUnit = rtDwarfCursor_GetU64(pCursor, 0);
817 }
818
819
820 /*
821 * Set the unit length, quitely fixing bad lengths.
822 */
823 pCursor->cbUnitLeft = (size_t)cbUnit;
824 if ( pCursor->cbUnitLeft > pCursor->cbLeft
825 || pCursor->cbUnitLeft != cbUnit)
826 pCursor->cbUnitLeft = pCursor->cbLeft;
827
828 return cbUnit;
829}
830
831
832/**
833 * Calculates the section offset corresponding to the current cursor position.
834 *
835 * @returns 32-bit section offset. If out of range, RTDWARFCURSOR::rc will be
836 * set and UINT32_MAX returned.
837 * @param pCursor The cursor.
838 */
839static uint32_t rtDwarfCursor_CalcSectOffsetU32(PRTDWARFCURSOR pCursor)
840{
841 size_t off = (uint8_t const *)pCursor->pDwarfMod->aSections[pCursor->enmSect].pv - pCursor->pb;
842 uint32_t offRet = (uint32_t)off;
843 if (offRet != off)
844 {
845 pCursor->rc = VERR_OUT_OF_RANGE;
846 offRet = UINT32_MAX;
847 }
848 return offRet;
849}
850
851
852/**
853 * Calculates an absolute cursor position from one relative to the current
854 * cursor position.
855 *
856 * @returns The absolute cursor position.
857 * @param pCursor The cursor.
858 * @param offRelative The relative position. Must be a positive
859 * offset.
860 */
861static uint8_t const *rtDwarfCursor_CalcPos(PRTDWARFCURSOR pCursor, size_t offRelative)
862{
863 if (offRelative > pCursor->cbUnitLeft)
864 {
865 pCursor->rc = VERR_DWARF_BAD_POS;
866 return NULL;
867 }
868 return pCursor->pb + offRelative;
869}
870
871
872/**
873 * Advances the cursor to the given position.
874 *
875 * @returns IPRT status code.
876 * @param pCursor The cursor.
877 * @param pbNewPos The new position - returned by
878 * rtDwarfCursor_CalcPos().
879 */
880static int rtDwarfCursor_AdvanceToPos(PRTDWARFCURSOR pCursor, uint8_t const *pbNewPos)
881{
882 if (RT_FAILURE(pCursor->rc))
883 return pCursor->rc;
884 AssertPtr(pbNewPos);
885 if ((uintptr_t)pbNewPos < (uintptr_t)pCursor->pb)
886 return pCursor->rc = VERR_DWARF_BAD_POS;
887
888 uintptr_t cbAdj = (uintptr_t)pbNewPos - (uintptr_t)pCursor->pb;
889 if (RT_UNLIKELY(cbAdj > pCursor->cbUnitLeft))
890 {
891 AssertFailed();
892 pCursor->rc = VERR_DWARF_BAD_POS;
893 cbAdj = pCursor->cbUnitLeft;
894 }
895
896 pCursor->cbUnitLeft -= cbAdj;
897 pCursor->cbLeft -= cbAdj;
898 pCursor->pb += cbAdj;
899 return pCursor->rc;
900}
901
902
903/**
904 * Check if the cursor is at the end of the current DWARF unit.
905 *
906 * @returns @c true if at the end, @c false if not.
907 * @param pCursor The cursor.
908 */
909static bool rtDwarfCursor_IsAtEndOfUnit(PRTDWARFCURSOR pCursor)
910{
911 return !pCursor->cbUnitLeft;
912}
913
914
915/**
916 * Skips to the end of the current unit.
917 *
918 * @returns IPRT status code.
919 * @param pCursor The cursor.
920 */
921static int rtDwarfCursor_SkipUnit(PRTDWARFCURSOR pCursor)
922{
923 pCursor->pb += pCursor->cbUnitLeft;
924 pCursor->cbLeft -= pCursor->cbUnitLeft;
925 pCursor->cbUnitLeft = 0;
926 return pCursor->rc;
927}
928
929
930/**
931 * Check if the cursor is at the end of the section (or whatever the cursor is
932 * processing).
933 *
934 * @returns @c true if at the end, @c false if not.
935 * @param pCursor The cursor.
936 */
937static bool rtDwarfCursor_IsAtEnd(PRTDWARFCURSOR pCursor)
938{
939 return !pCursor->cbLeft;
940}
941
942
943/**
944 * Initialize a section reader cursor.
945 *
946 * @returns IPRT status code.
947 * @param pCursor The cursor.
948 * @param pThis The dwarf module.
949 * @param enmSect The name of the section to read.
950 */
951static int rtDwarfCursor_Init(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
952{
953 int rc = rtDbgModDwarfLoadSection(pThis, enmSect);
954 if (RT_FAILURE(rc))
955 return rc;
956
957 pCursor->enmSect = enmSect;
958 pCursor->pbStart = (uint8_t const *)pThis->aSections[enmSect].pv;
959 pCursor->pb = pCursor->pbStart;
960 pCursor->cbLeft = pThis->aSections[enmSect].cb;
961 pCursor->cbUnitLeft = pCursor->cbLeft;
962 pCursor->pDwarfMod = pThis;
963 pCursor->f64bitDwarf = false;
964 /** @todo ask the image about the endian used as well as the address
965 * width. */
966 pCursor->fNativEndian = true;
967 pCursor->cbNativeAddr = 4;
968 pCursor->rc = VINF_SUCCESS;
969
970 return VINF_SUCCESS;
971}
972
973
974/**
975 * Initialize a section reader cursor with an offset.
976 *
977 * @returns IPRT status code.
978 * @param pCursor The cursor.
979 * @param pThis The dwarf module.
980 * @param enmSect The name of the section to read.
981 * @param offSect The offset into the section.
982 */
983static int rtDwarfCursor_InitWithOffset(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis,
984 krtDbgModDwarfSect enmSect, uint32_t offSect)
985{
986 if (offSect > pThis->aSections[enmSect].cb)
987 return VERR_DWARF_BAD_POS;
988
989 int rc = rtDwarfCursor_Init(pCursor, pThis, enmSect);
990 if (RT_SUCCESS(rc))
991 {
992 pCursor->pbStart += offSect;
993 pCursor->pb += offSect;
994 pCursor->cbLeft -= offSect;
995 pCursor->cbUnitLeft -= offSect;
996 }
997
998 return rc;
999}
1000
1001
1002/**
1003 * Deletes a section reader initialized by rtDwarfCursor_Init.
1004 *
1005 * @param pCursor The section reader.
1006 */
1007static void rtDwarfCursor_Delete(PRTDWARFCURSOR pCursor)
1008{
1009 /* ... and a drop of poison. */
1010 pCursor->pb = NULL;
1011 pCursor->cbLeft = ~(size_t)0;
1012 pCursor->cbUnitLeft = ~(size_t)0;
1013 pCursor->pDwarfMod = NULL;
1014 pCursor->rc = VERR_INTERNAL_ERROR_4;
1015}
1016
1017
1018/*
1019 *
1020 * DWARF Line Numbers.
1021 * DWARF Line Numbers.
1022 * DWARF Line Numbers.
1023 *
1024 */
1025
1026
1027/**
1028 * Defines a file name.
1029 *
1030 * @returns IPRT status code.
1031 * @param pLnState The line number program state.
1032 * @param pszFilename The name of the file.
1033 * @param idxInc The include path index.
1034 */
1035static int rtDwarfLine_DefineFileName(PRTDWARFLINESTATE pLnState, const char *pszFilename, uint64_t idxInc)
1036{
1037 /*
1038 * Resize the array if necessary.
1039 */
1040 uint32_t iFileName = pLnState->cFileNames;
1041 if ((iFileName % 2) == 0)
1042 {
1043 void *pv = RTMemRealloc(pLnState->papszFileNames, sizeof(pLnState->papszFileNames[0]) * (iFileName + 2));
1044 if (!pv)
1045 return VERR_NO_MEMORY;
1046 pLnState->papszFileNames = (char **)pv;
1047 }
1048
1049 /*
1050 * Add the file name.
1051 */
1052 if ( pszFilename[0] == '/'
1053 || pszFilename[0] == '\\'
1054 || (RT_C_IS_ALPHA(pszFilename[0]) && pszFilename[1] == ':') )
1055 pLnState->papszFileNames[iFileName] = RTStrDup(pszFilename);
1056 else if (idxInc < pLnState->cIncPaths)
1057 pLnState->papszFileNames[iFileName] = RTPathJoinA(pLnState->papszIncPaths[idxInc], pszFilename);
1058 else
1059 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
1060 if (!pLnState->papszFileNames[iFileName])
1061 return VERR_NO_STR_MEMORY;
1062 pLnState->cFileNames = iFileName + 1;
1063
1064 /*
1065 * Sanitize the name.
1066 */
1067 int rc = rtDbgModDwarfStringToUtf8(pLnState->pDwarfMod, &pLnState->papszFileNames[iFileName]);
1068 Log((" File #%02u = '%s'\n", iFileName, pLnState->papszFileNames[iFileName]));
1069 return rc;
1070}
1071
1072
1073/**
1074 * Adds a line to the table and resets parts of the state (DW_LNS_copy).
1075 *
1076 * @returns IPRT status code
1077 * @param pLnState The line number program state.
1078 */
1079static int rtDwarfLine_AddLine(PRTDWARFLINESTATE pLnState)
1080{
1081 const char *pszFile = pLnState->Regs.iFile < pLnState->cFileNames
1082 ? pLnState->papszFileNames[pLnState->Regs.iFile]
1083 : "<bad file name index>";
1084 RTDBGSEGIDX iSeg;
1085 RTUINTPTR offSeg;
1086 int rc = rtDbgModDwarfLinkAddressToSegOffset(pLnState->pDwarfMod, pLnState->Regs.uAddress, &iSeg, &offSeg);
1087 if (RT_SUCCESS(rc))
1088 {
1089 Log2(("rtDwarfLine_AddLine: %x:%08llx (%#llx) %s(%d)\n", iSeg, offSeg, pLnState->Regs.uAddress, pszFile, pLnState->Regs.uLine));
1090 rc = RTDbgModLineAdd(pLnState->pDwarfMod->hCnt, pszFile, pLnState->Regs.uLine, iSeg, offSeg, NULL);
1091
1092 /* Ignore address conflicts for now. */
1093 if (rc == VERR_DBG_ADDRESS_CONFLICT)
1094 rc = VINF_SUCCESS;
1095 }
1096
1097 pLnState->Regs.fBasicBlock = false;
1098 pLnState->Regs.fPrologueEnd = false;
1099 pLnState->Regs.fEpilogueBegin = false;
1100 pLnState->Regs.uDiscriminator = 0;
1101 return rc;
1102}
1103
1104
1105/**
1106 * Reset the program to the start-of-sequence state.
1107 *
1108 * @param pLnState The line number program state.
1109 */
1110static void rtDwarfLine_ResetState(PRTDWARFLINESTATE pLnState)
1111{
1112 pLnState->Regs.uAddress = 0;
1113 pLnState->Regs.idxOp = 0;
1114 pLnState->Regs.iFile = 1;
1115 pLnState->Regs.uLine = 1;
1116 pLnState->Regs.uColumn = 0;
1117 pLnState->Regs.fIsStatement = RT_BOOL(pLnState->Hdr.u8DefIsStmt);
1118 pLnState->Regs.fBasicBlock = false;
1119 pLnState->Regs.fEndSequence = false;
1120 pLnState->Regs.fPrologueEnd = false;
1121 pLnState->Regs.fEpilogueBegin = false;
1122 pLnState->Regs.uIsa = 0;
1123 pLnState->Regs.uDiscriminator = 0;
1124}
1125
1126
1127/**
1128 * Runs the line number program.
1129 *
1130 * @returns IPRT status code.
1131 * @param pLnState The line number program state.
1132 * @param pCursor The cursor.
1133 */
1134static int rtDwarfLine_RunProgram(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
1135{
1136 LogFlow(("rtDwarfLine_RunProgram: cbUnitLeft=%zu\n", pCursor->cbUnitLeft));
1137
1138 int rc = VINF_SUCCESS;
1139 rtDwarfLine_ResetState(pLnState);
1140
1141 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
1142 {
1143 uint8_t bOpCode = rtDwarfCursor_GetUByte(pCursor, DW_LNS_extended);
1144 if (bOpCode > pLnState->Hdr.u8OpcodeBase)
1145 {
1146 /*
1147 * Special opcode.
1148 */
1149 uint8_t const bLogOpCode = bOpCode; NOREF(bLogOpCode);
1150 bOpCode -= pLnState->Hdr.u8OpcodeBase;
1151
1152 int32_t const cLineDelta = bOpCode % pLnState->Hdr.u8LineRange + (int32_t)pLnState->Hdr.s8LineBase;
1153 bOpCode /= pLnState->Hdr.u8LineRange;
1154
1155 uint64_t uTmp = bOpCode + pLnState->Regs.idxOp + bOpCode;
1156 uint64_t const cAddressDelta = uTmp / pLnState->Hdr.cMaxOpsPerInstr * pLnState->Hdr.cbMinInstr;
1157 uint64_t const cOpIndexDelta = uTmp % pLnState->Hdr.cMaxOpsPerInstr;
1158
1159 pLnState->Regs.uLine += cLineDelta;
1160 pLnState->Regs.uAddress += cAddressDelta;
1161 pLnState->Regs.idxOp += cOpIndexDelta;
1162 Log2(("DW Special Opcode %#04x: uLine + %d => %u; uAddress + %#llx => %#llx; idxOp + %#llx => %#llx\n",
1163 bLogOpCode, cLineDelta, pLnState->Regs.uLine, cAddressDelta, pLnState->Regs.uAddress,
1164 cOpIndexDelta, pLnState->Regs.idxOp));
1165
1166 rc = rtDwarfLine_AddLine(pLnState);
1167 }
1168 else
1169 {
1170 switch (bOpCode)
1171 {
1172 /*
1173 * Standard opcode.
1174 */
1175 case DW_LNS_copy:
1176 Log2(("DW_LNS_copy\n"));
1177 rc = rtDwarfLine_AddLine(pLnState);
1178 break;
1179
1180 case DW_LNS_advance_pc:
1181 {
1182 uint64_t u64Adv = rtDwarfCursor_GetULeb128(pCursor, 0);
1183 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u64Adv) / pLnState->Hdr.cMaxOpsPerInstr
1184 * pLnState->Hdr.cbMinInstr;
1185 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + u64Adv) % pLnState->Hdr.cMaxOpsPerInstr;
1186 Log2(("DW_LNS_advance_pc\n"));
1187 break;
1188 }
1189
1190 case DW_LNS_advance_line:
1191 {
1192 int32_t cLineDelta = rtDwarfCursor_GetSLeb128AsS32(pCursor, 0);
1193 pLnState->Regs.uLine += cLineDelta;
1194 Log2(("DW_LNS_advance_line: uLine + %d => %u\n", cLineDelta, pLnState->Regs.uLine));
1195 break;
1196 }
1197
1198 case DW_LNS_set_file:
1199 pLnState->Regs.iFile = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1200 Log2(("DW_LNS_set_file: iFile=%u\n", pLnState->Regs.iFile));
1201 break;
1202
1203 case DW_LNS_set_column:
1204 pLnState->Regs.uColumn = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1205 Log2(("DW_LNS_set_column\n"));
1206 break;
1207
1208 case DW_LNS_negate_stmt:
1209 pLnState->Regs.fIsStatement = !pLnState->Regs.fIsStatement;
1210 Log2(("DW_LNS_negate_stmt\n"));
1211 break;
1212
1213 case DW_LNS_set_basic_block:
1214 pLnState->Regs.fBasicBlock = true;
1215 Log2(("DW_LNS_set_basic_block\n"));
1216 break;
1217
1218 case DW_LNS_const_add_pc:
1219 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + 255) / pLnState->Hdr.cMaxOpsPerInstr
1220 * pLnState->Hdr.cbMinInstr;
1221 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + 255) % pLnState->Hdr.cMaxOpsPerInstr;
1222 Log2(("DW_LNS_const_add_pc\n"));
1223 break;
1224
1225 case DW_LNS_fixed_advance_pc:
1226 pLnState->Regs.uAddress += rtDwarfCursor_GetUHalf(pCursor, 0);
1227 pLnState->Regs.idxOp = 0;
1228 Log2(("DW_LNS_fixed_advance_pc\n"));
1229 break;
1230
1231 case DW_LNS_set_prologue_end:
1232 pLnState->Regs.fPrologueEnd = true;
1233 Log2(("DW_LNS_set_prologue_end\n"));
1234 break;
1235
1236 case DW_LNS_set_epilogue_begin:
1237 pLnState->Regs.fEpilogueBegin = true;
1238 Log2(("DW_LNS_set_epilogue_begin\n"));
1239 break;
1240
1241 case DW_LNS_set_isa:
1242 pLnState->Regs.uIsa = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1243 Log2(("DW_LNS_set_isa %#x\n", pLnState->Regs.uIsa));
1244 break;
1245
1246 default:
1247 {
1248 unsigned cOpsToSkip = pLnState->Hdr.pacStdOperands[bOpCode - 1];
1249 Log2(("rtDwarfLine_RunProgram: Unknown standard opcode %#x, %#x operands\n", bOpCode, cOpsToSkip));
1250 while (cOpsToSkip-- > 0)
1251 rc = rtDwarfCursor_SkipLeb128(pCursor);
1252 break;
1253 }
1254
1255 /*
1256 * Extended opcode.
1257 */
1258 case DW_LNS_extended:
1259 {
1260 /* The instruction has a length prefix. */
1261 uint64_t cbInstr = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
1262 if (RT_FAILURE(pCursor->rc))
1263 return pCursor->rc;
1264 if (cbInstr > pCursor->cbUnitLeft)
1265 return VERR_DWARF_BAD_LNE;
1266 uint8_t const * const pbEndOfInstr = rtDwarfCursor_CalcPos(pCursor, cbInstr);
1267
1268 /* Get the opcode and deal with it if we know it. */
1269 bOpCode = rtDwarfCursor_GetUByte(pCursor, 0);
1270 switch (bOpCode)
1271 {
1272 case DW_LNE_end_sequence:
1273#if 0 /* No need for this, I think. */
1274 pLnState->Regs.fEndSequence = true;
1275 rc = rtDwarfLine_AddLine(pLnState);
1276#endif
1277 rtDwarfLine_ResetState(pLnState);
1278 Log2(("DW_LNE_end_sequence\n"));
1279 break;
1280
1281 case DW_LNE_set_address:
1282 switch (cbInstr - 1)
1283 {
1284 case 2: pLnState->Regs.uAddress = rtDwarfCursor_GetU16(pCursor, UINT16_MAX); break;
1285 case 4: pLnState->Regs.uAddress = rtDwarfCursor_GetU32(pCursor, UINT32_MAX); break;
1286 case 8: pLnState->Regs.uAddress = rtDwarfCursor_GetU64(pCursor, UINT64_MAX); break;
1287 default:
1288 AssertMsgFailed(("%d\n", cbInstr));
1289 pLnState->Regs.uAddress = rtDwarfCursor_GetNativeUOff(pCursor, UINT64_MAX);
1290 break;
1291 }
1292 pLnState->Regs.idxOp = 0;
1293 Log2(("DW_LNE_set_address: %#llx\n", pLnState->Regs.uAddress));
1294 break;
1295
1296 case DW_LNE_define_file:
1297 {
1298 const char *pszFilename = rtDwarfCursor_GetSZ(pCursor, NULL);
1299 uint32_t idxInc = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
1300 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
1301 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
1302 Log2(("DW_LNE_define_file: {%d}/%s\n", idxInc, pszFilename));
1303
1304 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
1305 if (RT_SUCCESS(rc))
1306 rc = rtDwarfLine_DefineFileName(pLnState, pszFilename, idxInc);
1307 }
1308
1309 case DW_LNE_set_descriminator:
1310 pLnState->Regs.uDiscriminator = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
1311 Log2(("DW_LNE_set_descriminator: %u\n", pLnState->Regs.uDiscriminator));
1312 break;
1313
1314 default:
1315 Log2(("rtDwarfLine_RunProgram: Unknown extended opcode %#x, length %#x\n", bOpCode, cbInstr));
1316 break;
1317 }
1318
1319 /* Advance the cursor to the end of the instruction . */
1320 rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
1321 break;
1322 }
1323 }
1324 }
1325
1326 /*
1327 * Check the status before looping.
1328 */
1329 if (RT_FAILURE(rc))
1330 return rc;
1331 if (RT_FAILURE(pCursor->rc))
1332 return pCursor->rc;
1333 }
1334 return rc;
1335}
1336
1337
1338/**
1339 * Reads the include directories for a line number unit.
1340 *
1341 * @returns IPRT status code
1342 * @param pLnState The line number program state.
1343 * @param pCursor The cursor.
1344 */
1345static int rtDwarfLine_ReadFileNames(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
1346{
1347 int rc = rtDwarfLine_DefineFileName(pLnState, "/<bad-zero-file-name-entry>", 0);
1348 if (RT_FAILURE(rc))
1349 return rc;
1350
1351 for (;;)
1352 {
1353 const char *psz = rtDwarfCursor_GetSZ(pCursor, NULL);
1354 if (!*psz)
1355 break;
1356
1357 uint64_t idxInc = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
1358 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
1359 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
1360
1361 rc = rtDwarfLine_DefineFileName(pLnState, psz, idxInc);
1362 if (RT_FAILURE(rc))
1363 return rc;
1364 }
1365 return pCursor->rc;
1366}
1367
1368
1369/**
1370 * Reads the include directories for a line number unit.
1371 *
1372 * @returns IPRT status code
1373 * @param pLnState The line number program state.
1374 * @param pCursor The cursor.
1375 */
1376static int rtDwarfLine_ReadIncludePaths(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
1377{
1378 const char *psz = ""; /* The zeroth is the unit dir. */
1379 for (;;)
1380 {
1381 if ((pLnState->cIncPaths % 2) == 0)
1382 {
1383 void *pv = RTMemRealloc(pLnState->papszIncPaths, sizeof(pLnState->papszIncPaths[0]) * (pLnState->cIncPaths + 2));
1384 if (!pv)
1385 return VERR_NO_MEMORY;
1386 pLnState->papszIncPaths = (const char **)pv;
1387 }
1388 Log((" Path #%02u = '%s'\n", pLnState->cIncPaths, psz));
1389 pLnState->papszIncPaths[pLnState->cIncPaths] = psz;
1390 pLnState->cIncPaths++;
1391
1392 psz = rtDwarfCursor_GetSZ(pCursor, NULL);
1393 if (!*psz)
1394 break;
1395 }
1396
1397 return pCursor->rc;
1398}
1399
1400
1401/**
1402 * Explodes the line number table for a compilation unit.
1403 *
1404 * @returns IPRT status code
1405 * @param pThis The DWARF instance.
1406 * @param pCursor The cursor to read the line number information
1407 * via.
1408 */
1409static int rtDwarfLine_ExplodeUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor)
1410{
1411 RTDWARFLINESTATE LnState;
1412 RT_ZERO(LnState);
1413 LnState.pDwarfMod = pThis;
1414
1415 /*
1416 * Parse the header.
1417 */
1418 rtDwarfCursor_GetInitalLength(pCursor);
1419 LnState.Hdr.uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
1420 if ( LnState.Hdr.uVer < 2
1421 || LnState.Hdr.uVer > 4)
1422 return rtDwarfCursor_SkipUnit(pCursor);
1423
1424 LnState.Hdr.offFirstOpcode = rtDwarfCursor_GetUOff(pCursor, 0);
1425 uint8_t const * const pbFirstOpcode = rtDwarfCursor_CalcPos(pCursor, LnState.Hdr.offFirstOpcode);
1426
1427 LnState.Hdr.cbMinInstr = rtDwarfCursor_GetUByte(pCursor, 0);
1428 if (LnState.Hdr.uVer >= 4)
1429 LnState.Hdr.cMaxOpsPerInstr = rtDwarfCursor_GetUByte(pCursor, 0);
1430 else
1431 LnState.Hdr.cMaxOpsPerInstr = 1;
1432 LnState.Hdr.u8DefIsStmt = rtDwarfCursor_GetUByte(pCursor, 0);
1433 LnState.Hdr.s8LineBase = rtDwarfCursor_GetSByte(pCursor, 0);
1434 LnState.Hdr.u8LineRange = rtDwarfCursor_GetUByte(pCursor, 0);
1435 LnState.Hdr.u8OpcodeBase = rtDwarfCursor_GetUByte(pCursor, 0);
1436
1437 if ( !LnState.Hdr.u8OpcodeBase
1438 || !LnState.Hdr.cMaxOpsPerInstr
1439 || !LnState.Hdr.u8LineRange
1440 || LnState.Hdr.u8DefIsStmt > 1)
1441 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
1442 Log2(("DWARF Line number header:\n"
1443 " uVer %d\n"
1444 " offFirstOpcode %#llx\n"
1445 " cbMinInstr %u\n"
1446 " cMaxOpsPerInstr %u\n"
1447 " u8DefIsStmt %u\n"
1448 " s8LineBase %d\n"
1449 " u8LineRange %u\n"
1450 " u8OpcodeBase %u\n",
1451 LnState.Hdr.uVer, LnState.Hdr.offFirstOpcode, LnState.Hdr.cbMinInstr, LnState.Hdr.cMaxOpsPerInstr,
1452 LnState.Hdr.u8DefIsStmt, LnState.Hdr.s8LineBase, LnState.Hdr.u8LineRange, LnState.Hdr.u8OpcodeBase));
1453
1454 LnState.Hdr.pacStdOperands = pCursor->pb;
1455 for (uint8_t iStdOpcode = 1; iStdOpcode < LnState.Hdr.u8OpcodeBase; iStdOpcode++)
1456 rtDwarfCursor_GetUByte(pCursor, 0);
1457
1458 int rc = pCursor->rc;
1459 if (RT_SUCCESS(rc))
1460 rc = rtDwarfLine_ReadIncludePaths(&LnState, pCursor);
1461 if (RT_SUCCESS(rc))
1462 rc = rtDwarfLine_ReadFileNames(&LnState, pCursor);
1463
1464 /*
1465 * Run the program....
1466 */
1467 if (RT_SUCCESS(rc))
1468 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbFirstOpcode);
1469 if (RT_SUCCESS(rc))
1470 rc = rtDwarfLine_RunProgram(&LnState, pCursor);
1471
1472 /*
1473 * Clean up.
1474 */
1475 size_t i = LnState.cFileNames;
1476 while (i-- > 0)
1477 RTStrFree(LnState.papszFileNames[i]);
1478 RTMemFree(LnState.papszFileNames);
1479 RTMemFree(LnState.papszIncPaths);
1480
1481 Assert(rtDwarfCursor_IsAtEndOfUnit(pCursor) || RT_FAILURE(rc));
1482 return rc;
1483}
1484
1485
1486/**
1487 * Explodes the line number table.
1488 *
1489 * The line numbers are insered into the debug info container.
1490 *
1491 * @returns IPRT status code
1492 * @param pThis The DWARF instance.
1493 */
1494static int rtDwarfLine_ExplodeAll(PRTDBGMODDWARF pThis)
1495{
1496 if (!pThis->aSections[krtDbgModDwarfSect_line].fPresent)
1497 return VINF_SUCCESS;
1498
1499 RTDWARFCURSOR Cursor;
1500 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_line);
1501 if (RT_FAILURE(rc))
1502 return rc;
1503
1504 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
1505 && RT_SUCCESS(rc))
1506 rc = rtDwarfLine_ExplodeUnit(pThis, &Cursor);
1507
1508 rtDwarfCursor_Delete(&Cursor);
1509 return rc;
1510}
1511
1512
1513/*
1514 *
1515 * DWARF Abbreviations.
1516 * DWARF Abbreviations.
1517 * DWARF Abbreviations.
1518 *
1519 */
1520
1521/**
1522 * Deals with a cache miss in rtDwarfAbbrev_Lookup.
1523 *
1524 * @returns Pointer to abbreviation cache entry (read only). May be rendered
1525 * invalid by subsequent calls to this function.
1526 * @param pThis The DWARF instance.
1527 * @param uCode The abbreviation code to lookup.
1528 */
1529static PCRTDBGMODDWARFABBREV rtDwarfAbbrev_LookupMiss(PRTDBGMODDWARF pThis, uint32_t uCode)
1530{
1531 /*
1532 * There is no entry with code zero.
1533 */
1534 if (!uCode)
1535 return NULL;
1536
1537 /*
1538 * Resize the cache array if the code is considered cachable.
1539 */
1540 bool fFillCache = true;
1541 if (pThis->cCachedAbbrevsAlloced < uCode)
1542 {
1543 if (uCode > _64K)
1544 fFillCache = false;
1545 else
1546 {
1547 uint32_t cNew = RT_ALIGN(uCode, 64);
1548 void *pv = RTMemRealloc(pThis->paCachedAbbrevs, sizeof(pThis->paCachedAbbrevs[0]) * cNew);
1549 if (!pv)
1550 fFillCache = false;
1551 else
1552 {
1553 pThis->cCachedAbbrevsAlloced = cNew;
1554 pThis->paCachedAbbrevs = (PRTDBGMODDWARFABBREV)pv;
1555 }
1556 }
1557 }
1558
1559 /*
1560 * Walk the abbreviations till we find the desired code.
1561 */
1562 RTDWARFCURSOR Cursor;
1563 int rc = rtDwarfCursor_InitWithOffset(&Cursor, pThis, krtDbgModDwarfSect_abbrev, pThis->offCachedAbbrev);
1564 if (RT_FAILURE(rc))
1565 return NULL;
1566
1567 PRTDBGMODDWARFABBREV pRet = NULL;
1568 if (fFillCache)
1569 {
1570 /*
1571 * Search for the entry and fill the cache while doing so.
1572 */
1573 for (;;)
1574 {
1575 /* Read the 'header'. */
1576 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1577 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1578 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
1579 if (RT_FAILURE(Cursor.rc))
1580 break;
1581 if ( uCurTag > 0xffff
1582 || uChildren > 1)
1583 {
1584 Cursor.rc = VERR_DWARF_BAD_ABBREV;
1585 break;
1586 }
1587
1588 /* Cache it? */
1589 if (uCurCode >= pThis->cCachedAbbrevsAlloced)
1590 {
1591 PRTDBGMODDWARFABBREV pEntry = &pThis->paCachedAbbrevs[uCurCode - 1];
1592 while (pThis->cCachedAbbrevs < uCurCode)
1593 {
1594 pThis->paCachedAbbrevs[pThis->cCachedAbbrevs].fFilled = false;
1595 pThis->cCachedAbbrevs++;
1596 }
1597
1598 pEntry->fFilled = true;
1599 pEntry->fChildren = RT_BOOL(uChildren);
1600 pEntry->uTag = uCurTag;
1601 pEntry->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
1602
1603 if (uCurCode == uCode)
1604 {
1605 pRet = pEntry;
1606 if (uCurCode == pThis->cCachedAbbrevsAlloced)
1607 break;
1608 }
1609 }
1610
1611 /* Skip the specification. */
1612 uint32_t uAttr, uForm;
1613 do
1614 {
1615 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1616 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1617 } while (uAttr != 0 && uForm != 0);
1618 if (RT_FAILURE(Cursor.rc))
1619 break;
1620
1621 /* Done? (Maximize cache filling.) */
1622 if ( pRet != NULL
1623 && uCurCode >= pThis->cCachedAbbrevsAlloced)
1624 break;
1625 }
1626 }
1627 else
1628 {
1629 /*
1630 * Search for the entry with the desired code, no cache filling.
1631 */
1632 for (;;)
1633 {
1634 /* Read the 'header'. */
1635 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1636 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1637 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
1638 if (RT_FAILURE(Cursor.rc))
1639 break;
1640 if ( uCurTag > 0xffff
1641 || uChildren > 1)
1642 {
1643 Cursor.rc = VERR_DWARF_BAD_ABBREV;
1644 break;
1645 }
1646
1647 /* Do we have a match? */
1648 if (uCurCode == uCode)
1649 {
1650 pRet = &pThis->LookupAbbrev;
1651 pRet->fFilled = true;
1652 pRet->fChildren = RT_BOOL(uChildren);
1653 pRet->uTag = uCurTag;
1654 pRet->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
1655 break;
1656 }
1657
1658 /* Skip the specification. */
1659 uint32_t uAttr, uForm;
1660 do
1661 {
1662 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1663 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
1664 } while (uAttr != 0 && uForm != 0);
1665 if (RT_FAILURE(Cursor.rc))
1666 break;
1667 }
1668 }
1669
1670 rtDwarfCursor_Delete(&Cursor);
1671 return pRet;
1672}
1673
1674
1675/**
1676 * Looks up an abbreviation.
1677 *
1678 * @returns Pointer to abbreviation cache entry (read only). May be rendered
1679 * invalid by subsequent calls to this function.
1680 * @param pThis The DWARF instance.
1681 * @param uCode The abbreviation code to lookup.
1682 */
1683static PCRTDBGMODDWARFABBREV rtDwarfAbbrev_Lookup(PRTDBGMODDWARF pThis, uint32_t uCode)
1684{
1685 if ( uCode - 1 >= pThis->cCachedAbbrevs
1686 || !pThis->paCachedAbbrevs[uCode - 1].fFilled)
1687 return rtDwarfAbbrev_LookupMiss(pThis, uCode);
1688 return &pThis->paCachedAbbrevs[uCode - 1];
1689}
1690
1691
1692/**
1693 * Sets the abbreviation offset of the current unit.
1694 *
1695 * This will flush the cached abbreviation entries if the offset differs from
1696 * the previous unit.
1697 *
1698 * @param pThis The DWARF instance.
1699 * @param offAbbrev The offset into the abbreviation section.
1700 */
1701static void rtDwarfAbbrev_SetUnitOffset(PRTDBGMODDWARF pThis, uint32_t offAbbrev)
1702{
1703 if (pThis->offCachedAbbrev != offAbbrev)
1704 {
1705 pThis->offCachedAbbrev = offAbbrev;
1706 pThis->cCachedAbbrevs = 0;
1707 }
1708}
1709
1710
1711/*
1712 *
1713 * DWARF debug_info parser
1714 * DWARF debug_info parser
1715 * DWARF debug_info parser
1716 *
1717 */
1718
1719
1720static int rtDwarfInfo_LoadUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor)
1721{
1722 /*
1723 * Read the compilation unit header.
1724 */
1725 rtDwarfCursor_GetInitalLength(pCursor);
1726 uint16_t const uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
1727 if ( uVer < 2
1728 || uVer > 4)
1729 return rtDwarfCursor_SkipUnit(pCursor);
1730 uint64_t const offAbbrev = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
1731 uint8_t const cbNativeAddr = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
1732 if (RT_FAILURE(pCursor->rc))
1733 return pCursor->rc;
1734
1735 /*
1736 * Set up the abbreviation cache and store the native address size in the cursor.
1737 */
1738 if (offAbbrev > UINT32_MAX)
1739 return VERR_DWARF_BAD_INFO;
1740 rtDwarfAbbrev_SetUnitOffset(pThis, offAbbrev);
1741 pCursor->cbNativeAddr = cbNativeAddr;
1742
1743 /*
1744 * Parse DIEs.
1745 */
1746 int rc = VINF_SUCCESS;
1747 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
1748 {
1749 /** @todo The fun starts again here. */
1750 rtDwarfCursor_SkipUnit(pCursor);
1751
1752 /*
1753 * Check status codes before continuing.
1754 */
1755 if (RT_FAILURE(rc))
1756 return rc;
1757 if (RT_FAILURE(pCursor->rc))
1758 return pCursor->rc;
1759 }
1760
1761 return rc;
1762}
1763
1764
1765/**
1766 * Extracts the symbols.
1767 *
1768 * The symbols are insered into the debug info container.
1769 *
1770 * @returns IPRT status code
1771 * @param pThis The DWARF instance.
1772 */
1773static int rtDwarfInfo_LoadAll(PRTDBGMODDWARF pThis)
1774{
1775 RTDWARFCURSOR Cursor;
1776 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_info);
1777 if (RT_FAILURE(rc))
1778 return rc;
1779
1780 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
1781 && RT_SUCCESS(rc))
1782 rc = rtDwarfInfo_LoadUnit(pThis, &Cursor);
1783
1784 rtDwarfCursor_Delete(&Cursor);
1785 return rc;
1786}
1787
1788
1789
1790
1791/*
1792 *
1793 * DWARF Debug module implementation.
1794 * DWARF Debug module implementation.
1795 * DWARF Debug module implementation.
1796 *
1797 */
1798
1799
1800/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
1801static DECLCALLBACK(int) rtDbgModDwarf_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
1802 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
1803{
1804 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1805 return RTDbgModLineByAddr(pThis->hCnt, iSeg, off, poffDisp, pLineInfo);
1806}
1807
1808
1809/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
1810static DECLCALLBACK(int) rtDbgModDwarf_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
1811{
1812 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1813 return RTDbgModLineByOrdinal(pThis->hCnt, iOrdinal, pLineInfo);
1814}
1815
1816
1817/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
1818static DECLCALLBACK(uint32_t) rtDbgModDwarf_LineCount(PRTDBGMODINT pMod)
1819{
1820 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1821 return RTDbgModLineCount(pThis->hCnt);
1822}
1823
1824
1825/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
1826static DECLCALLBACK(int) rtDbgModDwarf_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
1827 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
1828{
1829 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1830 return RTDbgModLineAdd(pThis->hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
1831}
1832
1833
1834/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
1835static DECLCALLBACK(int) rtDbgModDwarf_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
1836 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1837{
1838 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1839 return RTDbgModSymbolByAddr(pThis->hCnt, iSeg, off, poffDisp, pSymInfo);
1840}
1841
1842
1843/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
1844static DECLCALLBACK(int) rtDbgModDwarf_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
1845 PRTDBGSYMBOL pSymInfo)
1846{
1847 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1848 Assert(!pszSymbol[cchSymbol]);
1849 return RTDbgModSymbolByName(pThis->hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
1850}
1851
1852
1853/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
1854static DECLCALLBACK(int) rtDbgModDwarf_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
1855{
1856 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1857 return RTDbgModSymbolByOrdinal(pThis->hCnt, iOrdinal, pSymInfo);
1858}
1859
1860
1861/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
1862static DECLCALLBACK(uint32_t) rtDbgModDwarf_SymbolCount(PRTDBGMODINT pMod)
1863{
1864 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1865 return RTDbgModSymbolCount(pThis->hCnt);
1866}
1867
1868
1869/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
1870static DECLCALLBACK(int) rtDbgModDwarf_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
1871 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
1872 uint32_t *piOrdinal)
1873{
1874 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1875 return RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
1876}
1877
1878
1879/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
1880static DECLCALLBACK(int) rtDbgModDwarf_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
1881{
1882 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1883 return RTDbgModSegmentByIndex(pThis->hCnt, iSeg, pSegInfo);
1884}
1885
1886
1887/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
1888static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_SegmentCount(PRTDBGMODINT pMod)
1889{
1890 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1891 return RTDbgModSegmentCount(pThis->hCnt);
1892}
1893
1894
1895/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
1896static DECLCALLBACK(int) rtDbgModDwarf_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
1897 uint32_t fFlags, PRTDBGSEGIDX piSeg)
1898{
1899 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1900 return RTDbgModSegmentAdd(pThis->hCnt, uRva, cb, pszName, fFlags, piSeg);
1901}
1902
1903
1904/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
1905static DECLCALLBACK(RTUINTPTR) rtDbgModDwarf_ImageSize(PRTDBGMODINT pMod)
1906{
1907 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1908 RTUINTPTR cb1 = RTDbgModImageSize(pThis->hCnt);
1909 RTUINTPTR cb2 = pMod->pImgVt->pfnImageSize(pMod);
1910 return RT_MAX(cb1, cb2);
1911}
1912
1913
1914/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
1915static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
1916{
1917 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1918 return RTDbgModRvaToSegOff(pThis->hCnt, uRva, poffSeg);
1919}
1920
1921
1922/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
1923static DECLCALLBACK(int) rtDbgModDwarf_Close(PRTDBGMODINT pMod)
1924{
1925 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
1926
1927 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
1928 if (pThis->aSections[iSect].pv)
1929 pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[iSect].cb, &pThis->aSections[iSect].pv);
1930
1931 RTDbgModRelease(pThis->hCnt);
1932 RTMemFree(pThis->paCachedAbbrevs);
1933 RTMemFree(pThis);
1934
1935 return VINF_SUCCESS;
1936}
1937
1938
1939/** @callback_method_impl{FNRTLDRENUMDBG} */
1940static DECLCALLBACK(int) rtDbgModDwarfEnumCallback(RTLDRMOD hLdrMod, uint32_t iDbgInfo, RTLDRDBGINFOTYPE enmType,
1941 uint16_t iMajorVer, uint16_t iMinorVer, const char *pszPartNm,
1942 RTFOFF offFile, RTLDRADDR LinkAddress, RTLDRADDR cb,
1943 const char *pszExtFile, void *pvUser)
1944{
1945 /*
1946 * Skip stuff we can't handle.
1947 */
1948 if ( enmType != RTLDRDBGINFOTYPE_DWARF
1949 || !pszPartNm
1950 || pszExtFile)
1951 return VINF_SUCCESS;
1952
1953 /*
1954 * Must have a part name starting with debug_ and possibly prefixed by dots
1955 * or underscores.
1956 */
1957 if (!strncmp(pszPartNm, ".debug_", sizeof(".debug_") - 1)) /* ELF */
1958 pszPartNm += sizeof(".debug_") - 1;
1959 else if (!strncmp(pszPartNm, "__debug_", sizeof("__debug_") - 1)) /* Mach-O */
1960 pszPartNm += sizeof("__debug_") - 1;
1961 else
1962 AssertMsgFailedReturn(("%s\n", pszPartNm), VINF_SUCCESS /*ignore*/);
1963
1964 /*
1965 * Figure out which part we're talking about.
1966 */
1967 krtDbgModDwarfSect enmSect;
1968 if (0) { /* dummy */ }
1969#define ELSE_IF_STRCMP_SET(a_Name) else if (!strcmp(pszPartNm, #a_Name)) enmSect = krtDbgModDwarfSect_ ## a_Name
1970 ELSE_IF_STRCMP_SET(abbrev);
1971 ELSE_IF_STRCMP_SET(aranges);
1972 ELSE_IF_STRCMP_SET(frame);
1973 ELSE_IF_STRCMP_SET(info);
1974 ELSE_IF_STRCMP_SET(inlined);
1975 ELSE_IF_STRCMP_SET(line);
1976 ELSE_IF_STRCMP_SET(loc);
1977 ELSE_IF_STRCMP_SET(macinfo);
1978 ELSE_IF_STRCMP_SET(pubnames);
1979 ELSE_IF_STRCMP_SET(pubtypes);
1980 ELSE_IF_STRCMP_SET(ranges);
1981 ELSE_IF_STRCMP_SET(str);
1982 ELSE_IF_STRCMP_SET(types);
1983#undef ELSE_IF_STRCMP_SET
1984 else
1985 {
1986 AssertMsgFailed(("%s\n", pszPartNm));
1987 return VINF_SUCCESS;
1988 }
1989
1990 /*
1991 * Record the section.
1992 */
1993 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
1994 AssertMsgReturn(!pThis->aSections[enmSect].fPresent, ("duplicate %s\n", pszPartNm), VINF_SUCCESS /*ignore*/);
1995
1996 pThis->aSections[enmSect].fPresent = true;
1997 pThis->aSections[enmSect].offFile = offFile;
1998 pThis->aSections[enmSect].pv = NULL;
1999 pThis->aSections[enmSect].cb = (size_t)cb;
2000 if (pThis->aSections[enmSect].cb != cb)
2001 pThis->aSections[enmSect].cb = ~(size_t)0;
2002
2003 return VINF_SUCCESS;
2004}
2005
2006
2007/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
2008static DECLCALLBACK(int) rtDbgModDwarf_TryOpen(PRTDBGMODINT pMod)
2009{
2010 /*
2011 * DWARF is only supported when part of an image.
2012 */
2013 if (!pMod->pImgVt)
2014 return VERR_DBG_NO_MATCHING_INTERPRETER;
2015
2016 /*
2017 * Enumerate the debug info in the module, looking for DWARF bits.
2018 */
2019 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)RTMemAllocZ(sizeof(*pThis));
2020 if (!pThis)
2021 return VERR_NO_MEMORY;
2022 pThis->pMod = pMod;
2023
2024 int rc = pMod->pImgVt->pfnEnumDbgInfo(pMod, rtDbgModDwarfEnumCallback, pThis);
2025 if (RT_SUCCESS(rc))
2026 {
2027 if (pThis->aSections[krtDbgModDwarfSect_info].fPresent)
2028 {
2029 /*
2030 * Extract / explode the data we want (symbols and line numbers)
2031 * storing them in a container module.
2032 */
2033 rc = RTDbgModCreate(&pThis->hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
2034 if (RT_SUCCESS(rc))
2035 {
2036 pMod->pvDbgPriv = pThis;
2037
2038 rc = rtDbgModHlpAddSegmentsFromImage(pMod);
2039 if (RT_SUCCESS(rc))
2040 rc = rtDwarfInfo_LoadAll(pThis);
2041 if (RT_SUCCESS(rc))
2042 rc = rtDwarfLine_ExplodeAll(pThis);
2043 if (RT_SUCCESS(rc))
2044 {
2045 /*
2046 * Free the cached abbreviations and unload all sections.
2047 */
2048 pThis->cCachedAbbrevs = pThis->cCachedAbbrevsAlloced = 0;
2049 RTMemFree(pThis->paCachedAbbrevs);
2050
2051 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
2052 if (pThis->aSections[iSect].pv)
2053 pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[iSect].cb,
2054 &pThis->aSections[iSect].pv);
2055
2056
2057 return VINF_SUCCESS;
2058 }
2059
2060 /* bail out. */
2061 RTDbgModRelease(pThis->hCnt);
2062 pMod->pvDbgPriv = NULL;
2063 }
2064 }
2065 else
2066 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
2067 }
2068 RTMemFree(pThis->paCachedAbbrevs);
2069 RTMemFree(pThis);
2070
2071 return rc;
2072}
2073
2074
2075
2076/** Virtual function table for the DWARF debug info reader. */
2077DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDwarf =
2078{
2079 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
2080 /*.fSupports = */ RT_DBGTYPE_DWARF,
2081 /*.pszName = */ "dwarf",
2082 /*.pfnTryOpen = */ rtDbgModDwarf_TryOpen,
2083 /*.pfnClose = */ rtDbgModDwarf_Close,
2084
2085 /*.pfnRvaToSegOff = */ rtDbgModDwarf_RvaToSegOff,
2086 /*.pfnImageSize = */ rtDbgModDwarf_ImageSize,
2087
2088 /*.pfnSegmentAdd = */ rtDbgModDwarf_SegmentAdd,
2089 /*.pfnSegmentCount = */ rtDbgModDwarf_SegmentCount,
2090 /*.pfnSegmentByIndex = */ rtDbgModDwarf_SegmentByIndex,
2091
2092 /*.pfnSymbolAdd = */ rtDbgModDwarf_SymbolAdd,
2093 /*.pfnSymbolCount = */ rtDbgModDwarf_SymbolCount,
2094 /*.pfnSymbolByOrdinal = */ rtDbgModDwarf_SymbolByOrdinal,
2095 /*.pfnSymbolByName = */ rtDbgModDwarf_SymbolByName,
2096 /*.pfnSymbolByAddr = */ rtDbgModDwarf_SymbolByAddr,
2097
2098 /*.pfnLineAdd = */ rtDbgModDwarf_LineAdd,
2099 /*.pfnLineCount = */ rtDbgModDwarf_LineCount,
2100 /*.pfnLineByOrdinal = */ rtDbgModDwarf_LineByOrdinal,
2101 /*.pfnLineByAddr = */ rtDbgModDwarf_LineByAddr,
2102
2103 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
2104};
2105
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