summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/dbg/dbgmodmapsym.cpp
blob: 0210f54c7e09bc5d1e2b463911ba791c5101a633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* $Id: dbgmodmapsym.cpp $ */
/** @file
 * IPRT - Debug Map Reader for MAPSYM files (used by SYMDBG from old MASM).
 *
 * MAPSYM is was the tool producing these files from linker map files for
 * use with SYMDBG (which shipped with MASM 3.0 (possibly earlier)), the OS/2
 * kernel debugger, and other tools.  The format is very limited and they had
 * to strip down the os2krnl.map file in later years to keep MAPSYM happy.
 */

/*
 * Copyright (C) 2009-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_DBG
#include <iprt/dbg.h>
#include "internal/iprt.h"

#include <iprt/err.h>
#include <iprt/ctype.h>
#include <iprt/file.h>
#include <iprt/log.h>
#include <iprt/mem.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include "internal/dbgmod.h"


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/** @name MAPSYM structures and constants.
 * @{ */

/** MAPSYM: Header structure.   */
typedef struct MAPSYMHDR
{
    uint16_t    off16NextMap;                   /**< 0x00: Offset of the next map divided by 16. */
    uint8_t     bFlags;                         /**< 0x02: Who really knows... */
    uint8_t     bReserved;                      /**< 0x03: Reserved / unknown. */
    uint16_t    uSegEntry;                      /**< 0x04: Some entrypoint/segment thing we don't care about. */
    uint16_t    cConsts;                        /**< 0x06: Constants referenced by offConstDef. */
    uint16_t    offConstDef;                    /**< 0x08: Offset to head of constant chain.  Not div 16? */
    uint16_t    cSegs;                          /**< 0x0a: Number of segments in the map. */
    uint16_t    off16SegDef;                    /**< 0x0c: Offset of the segment defintions divided by 16. */
    uint8_t     cchMaxSym;                      /**< 0x0e: Maximum symbol-name length. */
    uint8_t     cchModule;                      /**< 0x0f: Length of the module name. */
    RT_FLEXIBLE_ARRAY_EXTENSION
    char        achModule[RT_FLEXIBLE_ARRAY];   /**< 0x10: Module name, length given by cchModule. */
} MAPSYMHDR;

/** MAPSYM: Tail structure.   */
typedef struct MAPSYMTAIL
{
    uint16_t    offNextMap;                     /**< 0x00: Always zero (it's the tail, see). */
    uint8_t     bRelease;                       /**< 0x02: Minor version number. */
    uint8_t     bVersion;                       /**< 0x03: Major version number. */
} MAPSYMTAIL;

/** MAPSYM: Segment defintion.   */
typedef struct MAPSYMSEGDEF
{
    uint16_t    off16NextSeg;                  /**< 0x00: Offset of the next segment divided by 16. */
    uint16_t    cSymbols;                      /**< 0x02: Number of symbol offsets . */
    uint16_t    offSymbolOffsets;              /**< 0x04: Offset of the symbol offset table. Each entry is a 16-bit value giving
                                                *         the offset symbol relative to this structure. */
    uint16_t    au16Reserved0[4];              /**< 0x06: Reserved / unknown.
                                                * First byte/word seems to be 1-based segment number. */
    uint8_t     bFlags;                        /**< 0x0e: MAPSYMSEGDEF_F_32BIT or zero. */
    uint8_t     bReserved1;                    /**< 0x0f: Reserved / unknown. */
    uint16_t    offLineDef;                    /**< 0x10: Offset to the line defintions. */
    uint16_t    u16Reserved2;                  /**< 0x12: Reserved / unknown.  Often seen holding 0xff00. */
    uint8_t     cchSegName;                    /**< 0x14: Segment name length. */
    RT_FLEXIBLE_ARRAY_EXTENSION
    char        achSegName[RT_FLEXIBLE_ARRAY]; /**< 0x15: Segment name, length given by cchSegName. */
} MAPSYMSEGDEF;

#define MAPSYMSEGDEF_F_32BIT    UINT8_C(0x01)  /**< Indicates 32-bit segment rather than 16-bit, relevant for symbols. */
#define MAPSYMSEGDEF_F_UNKNOWN  UINT8_C(0x02)  /**< Set on all segments in os2krnlr.sym from ACP2. */

/** MAPSYM: 16-bit symbol   */
typedef struct MAPSYMSYMDEF16
{
    uint16_t    uValue;                        /**< 0x00: The symbol value (address). */
    uint8_t     cchName;                       /**< 0x02: Symbol name length. */
    char        achName[1];                    /**< 0x03: The symbol name, length give by cchName. */
} MAPSYMSYMDEF16;

/** MAPSYM: 16-bit symbol   */
typedef struct MAPSYMSYMDEF32
{
    uint32_t    uValue;                        /**< 0x00: The symbol value (address). */
    uint8_t     cchName;                       /**< 0x04: Symbol name length. */
    char        achName[1];                    /**< 0x05: The symbol name, length give by cchName. */
} MAPSYMSYMDEF32;

/** MAPSYM: Line number defintions. */
typedef struct MAPSYMLINEDEF
{
    uint16_t    off16NextLine;                 /**< 0x00: Offset to the next line defintion divided by 16. */
    uint16_t    uSegment;                      /**< 0x02: Guessing this must be segment number. */
    uint16_t    offLines;                      /**< 0x04: Offset to the line number array, relative to this structure. */
    uint16_t    cLines;                        /**< 0x08: Number of line numbers in the array. */
    uint8_t     cchSrcFile;                    /**< 0x0a: Length of source filename. */
    RT_FLEXIBLE_ARRAY_EXTENSION
    char        achSrcFile[RT_FLEXIBLE_ARRAY]; /**< 0x0b: Source filename, length given by cchSrcFile. */
} MAPSYMLINEDEF;

/** MAPSYM: 16-bit line numbers. */
typedef struct MAPSYMLINENO16
{
    uint16_t    offSeg;
    uint16_t    uLineNo;
} MAPSYMLINENO16;

/** @} */


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** Maximum number of segments we expect in a MAPSYM file. */
#define RTDBGMODMAPSYM_MAX_SEGMENTS 256



/** @interface_method_impl{RTDBGMODVTDBG,pfnUnwindFrame} */
static DECLCALLBACK(int) rtDbgModMapSym_UnwindFrame(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
{
    RT_NOREF(pMod, iSeg, off, pState);
    return VERR_DBG_NO_UNWIND_INFO;
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
static DECLCALLBACK(int) rtDbgModMapSym_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
                                               PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModLineByAddr(hCnt, iSeg, off, poffDisp, pLineInfo);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
static DECLCALLBACK(int) rtDbgModMapSym_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModLineByOrdinal(hCnt, iOrdinal, pLineInfo);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
static DECLCALLBACK(uint32_t) rtDbgModMapSym_LineCount(PRTDBGMODINT pMod)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModLineCount(hCnt);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
static DECLCALLBACK(int) rtDbgModMapSym_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
                                            uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    Assert(!pszFile[cchFile]); NOREF(cchFile);
    return RTDbgModLineAdd(hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
static DECLCALLBACK(int) rtDbgModMapSym_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
                                                 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModSymbolByAddr(hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
static DECLCALLBACK(int) rtDbgModMapSym_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
                                                 PRTDBGSYMBOL pSymInfo)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
    return RTDbgModSymbolByName(hCnt, pszSymbol, pSymInfo);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
static DECLCALLBACK(int) rtDbgModMapSym_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModSymbolByOrdinal(hCnt, iOrdinal, pSymInfo);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
static DECLCALLBACK(uint32_t) rtDbgModMapSym_SymbolCount(PRTDBGMODINT pMod)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModSymbolCount(hCnt);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
static DECLCALLBACK(int) rtDbgModMapSym_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
                                                  RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
                                                  uint32_t *piOrdinal)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
    return RTDbgModSymbolAdd(hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
static DECLCALLBACK(int) rtDbgModMapSym_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModSegmentByIndex(hCnt, iSeg, pSegInfo);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
static DECLCALLBACK(RTDBGSEGIDX) rtDbgModMapSym_SegmentCount(PRTDBGMODINT pMod)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModSegmentCount(hCnt);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
static DECLCALLBACK(int) rtDbgModMapSym_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
                                               size_t cchName, uint32_t fFlags, PRTDBGSEGIDX piSeg)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    Assert(!pszName[cchName]); NOREF(cchName);
    return RTDbgModSegmentAdd(hCnt, uRva, cb, pszName, fFlags, piSeg);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
static DECLCALLBACK(RTUINTPTR) rtDbgModMapSym_ImageSize(PRTDBGMODINT pMod)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModImageSize(hCnt);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
static DECLCALLBACK(RTDBGSEGIDX) rtDbgModMapSym_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    return RTDbgModRvaToSegOff(hCnt, uRva, poffSeg);
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
static DECLCALLBACK(int) rtDbgModMapSym_Close(PRTDBGMODINT pMod)
{
    RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
    RTDbgModRelease(hCnt);
    pMod->pvDbgPriv = NULL;
    return VINF_SUCCESS;
}


/**
 * Validate the module header.
 *
 * @returns true if valid, false if not.
 * @param   pHdr        The header.
 * @param   cbAvail     How much we've actually read.
 * @param   cbFile      The file size (relative to module header).
 */
static bool rtDbgModMapSymIsValidHeader(MAPSYMHDR const *pHdr, size_t cbAvail, uint64_t cbFile)
{
    if (cbAvail <= RT_UOFFSETOF(MAPSYMHDR, achModule))
        return false;

    if (pHdr->cSegs == 0)
        return false;
    if (pHdr->cSegs > RTDBGMODMAPSYM_MAX_SEGMENTS)
        return false;

    if (pHdr->off16SegDef == 0)
        return false;
    if (pHdr->off16SegDef * (uint32_t)16 >= cbFile)
        return false;

    if (pHdr->cchModule == 0)
        return false;
    if (pHdr->cchModule > 128) /* Note must be smaller than abPadding below in caller */
        return false;

    size_t cchMaxName = cbAvail - RT_UOFFSETOF(MAPSYMHDR, achModule);
    if (pHdr->cchModule > cchMaxName)
        return false;

    for (uint32_t i = 0; i < pHdr->cchModule; i++)
    {
        unsigned char const uch = pHdr->achModule[i];
        if (   uch <  0x20
            || uch >= 0x7f)
            return false;
    }

    return true;
}


/**
 * Validate the given segment definition.
 *
 * @returns true if valid, false if not.
 * @param   pSegDef     The segment definition structure.
 * @param   cbMax       Host many bytes are available starting with pSegDef.
 */
static bool rtDbgModMapSymIsValidSegDef(MAPSYMSEGDEF const *pSegDef, size_t cbMax)
{
    if (RT_UOFFSETOF(MAPSYMSEGDEF, achSegName) > cbMax)
        return false;
    if (pSegDef->cSymbols)
    {
        if (pSegDef->cSymbols > _32K)
        {
            Log(("rtDbgModMapSymIsValidSegDef: Too many symbols: %#x\n", pSegDef->cSymbols));
            return false;
        }

        if (pSegDef->offSymbolOffsets + (uint32_t)2 * pSegDef->cSymbols > cbMax)
        {
            Log(("rtDbgModMapSymIsValidSegDef: Bad symbol offset/count: %#x/%#x\n", pSegDef->offSymbolOffsets, pSegDef->cSymbols));
            return false;
        }
    }

    size_t cchMaxName = cbMax - RT_UOFFSETOF(MAPSYMHDR, achModule);
    if (pSegDef->cchSegName > cchMaxName)
    {
        Log(("rtDbgModMapSymIsValidSegDef: Bad segment name length\n"));
        return false;
    }

    for (uint32_t i = 0; i < pSegDef->cchSegName; i++)
    {
        unsigned char uch = pSegDef->achSegName[i];
        if (   uch <  0x20
            || uch >= 0x7f)
        {
            Log(("rtDbgModMapSymIsValidSegDef: Bad segment name: %.*Rhxs\n", pSegDef->cchSegName, pSegDef->achSegName));
            return false;
        }
    }

    return true;
}


/**
 * Fills @a hCnt with segments and symbols from the MAPSYM file.
 *
 * @note  We only support reading the first module, right now.
 */
static int rtDbgModMapSymReadIt(RTDBGMOD hCnt, uint8_t const *pbFile, size_t cbFile)
{
    /*
     * Revalidate the header.
     */
    MAPSYMHDR const *pHdr = (MAPSYMHDR const *)pbFile;
    if (!rtDbgModMapSymIsValidHeader(pHdr, cbFile, cbFile))
        return VERR_DBG_NO_MATCHING_INTERPRETER;
    Log(("rtDbgModMapSymReadIt: szModule='%.*s' cSegs=%u off16NextMap=%#x\n",
         pHdr->cchModule, pHdr->achModule, pHdr->cSegs, pHdr->off16NextMap));

    /*
     * Load each segment.
     */
    uint32_t       uRva       = 0;
    uint32_t const cSegs      = pHdr->cSegs;
    uint32_t       offSegment = pHdr->off16SegDef * (uint32_t)16;
    for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
    {
        if (offSegment >= cbFile)
            return VERR_DBG_NO_MATCHING_INTERPRETER;

        size_t const cbMax = cbFile - offSegment;
        MAPSYMSEGDEF const *pSegDef = (MAPSYMSEGDEF const *)&pbFile[offSegment];
        if (!rtDbgModMapSymIsValidSegDef(pSegDef, cbMax))
            return VERR_DBG_NO_MATCHING_INTERPRETER;

        Log(("rtDbgModMapSymReadIt:  Segment #%u: flags=%#x name='%.*s' symbols=%#x @ %#x next=%#x lines=@%#x (reserved: %#x %#x %#x %#x %#x %#x)\n",
             iSeg, pSegDef->bFlags, pSegDef->cchSegName, pSegDef->achSegName, pSegDef->cSymbols, pSegDef->offSymbolOffsets,
             pSegDef->off16NextSeg, pSegDef->offLineDef, pSegDef->au16Reserved0[0], pSegDef->au16Reserved0[1],
             pSegDef->au16Reserved0[2], pSegDef->au16Reserved0[3], pSegDef->bReserved1, pSegDef->u16Reserved2));

        /*
         * First symbol pass finds the largest symbol and uses that as the segment size.
         */
        uint32_t               cbSegmentEst = 0;
        uint32_t const         cSymbols     = pSegDef->cSymbols;
        uint16_t const * const paoffSymbols = (uint16_t const *)&pbFile[offSegment + pSegDef->offSymbolOffsets];
        bool const             fIs32Bit     = RT_BOOL(pSegDef->bFlags & MAPSYMSEGDEF_F_32BIT);
        uint32_t const         cbSymDef     = fIs32Bit ? 4 + 1 : 2 + 1;
        for (uint32_t iSymbol = 0; iSymbol < cSymbols; iSymbol++)
        {
            uint32_t off = paoffSymbols[iSymbol] + offSegment;
            if (off + cbSymDef <= cbFile)
            {
                uint32_t uValue = fIs32Bit ? *(uint32_t const *)&pbFile[off] : (uint32_t)*(uint16_t const *)&pbFile[off];
                if (uValue > cbSegmentEst)
                    cbSegmentEst = uValue;
            }
            else
                Log(("rtDbgModMapSymReadIt:  Bad symbol offset %#x\n", off));
        }

        /*
         * Add the segment.
         */
        char szName[256];
        memcpy(szName, pSegDef->achSegName, pSegDef->cchSegName);
        szName[pSegDef->cchSegName] = '\0';
        if (!pSegDef->cchSegName)
            RTStrPrintf(szName, sizeof(szName), "seg%02u", iSeg);

        RTDBGSEGIDX idxDbgSeg = iSeg;
        int rc = RTDbgModSegmentAdd(hCnt, uRva, cbSegmentEst, szName, 0 /*fFlags*/, &idxDbgSeg);
        if (RT_FAILURE(rc))
            return rc;

        uRva += cbSegmentEst;

        /*
         * The second symbol pass loads the symbol values and names.
         */
        for (uint32_t iSymbol = 0; iSymbol < cSymbols; iSymbol++)
        {
            uint32_t off = paoffSymbols[iSymbol] + offSegment;
            if (off + cbSymDef <= cbFile)
            {
                /* Get value: */
                uint32_t uValue = RT_MAKE_U16(pbFile[off], pbFile[off + 1]);
                off += 2;
                if (fIs32Bit)
                {
                    uValue |= RT_MAKE_U32_FROM_U8(0, 0, pbFile[off], pbFile[off + 1]);
                    off += 2;
                }

                /* Get name: */
                uint8_t cchName = pbFile[off++];
                if (off + cchName <= cbFile)
                {
                    memcpy(szName, &pbFile[off], cchName);
                    szName[cchName] = '\0';
                    RTStrPurgeEncoding(szName);
                }
                else
                    cchName = 0;
                if (cchName == 0)
                    RTStrPrintf(szName, sizeof(szName), "unknown_%u_%u", iSeg, iSymbol);

                /* Try add it: */
                rc = RTDbgModSymbolAdd(hCnt, szName, idxDbgSeg, uValue, 0 /*cb*/, 0 /*fFlags*/, NULL /*piOrdinal*/);
                if (RT_SUCCESS(rc))
                    Log7(("rtDbgModMapSymReadIt: %02x:%06x %s\n", idxDbgSeg, uValue, szName));
                else if (   rc == VERR_DBG_DUPLICATE_SYMBOL
                         || rc == VERR_DBG_ADDRESS_CONFLICT
                         || rc == VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE)
                    Log(("rtDbgModMapSymReadIt: %02x:%06x %s\n", idxDbgSeg, uValue, szName));
                else
                {
                    Log(("rtDbgModMapSymReadIt: Unexpected RTDbgModSymbolAdd failure: %Rrc - %02x:%06x %s\n",
                         rc, idxDbgSeg, uValue, szName));
                    return rc;
                }
            }
        }

        /* Next segment */
        offSegment = pSegDef->off16NextSeg * (uint32_t)16;
    }
    return VINF_SUCCESS;
}


/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
static DECLCALLBACK(int) rtDbgModMapSym_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
{
    NOREF(enmArch);

    /*
     * Fend off images.
     */
    if (   !pMod->pszDbgFile
        || pMod->pImgVt)
        return VERR_DBG_NO_MATCHING_INTERPRETER;
    pMod->pvDbgPriv = NULL;

    /*
     * Try open the file and check out the first header.
     */
    RTFILE hFile;
    int rc = RTFileOpen(&hFile, pMod->pszDbgFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
    if (RT_SUCCESS(rc))
    {
        uint64_t cbFile = 0;
        rc = RTFileQuerySize(hFile, &cbFile);
        if (   RT_SUCCESS(rc)
            && cbFile < _2M)
        {
            union
            {
                MAPSYMHDR Hdr;
                char      abPadding[sizeof(MAPSYMHDR) + 257]; /* rtDbgModMapSymIsValidHeader makes size assumptions.  */
            } uBuf;
            size_t cbToRead = (size_t)RT_MIN(cbFile, sizeof(uBuf));
            rc = RTFileReadAt(hFile, 0, &uBuf, RT_MIN(cbFile, sizeof(uBuf)), NULL);
            if (RT_SUCCESS(rc))
            {
                if (rtDbgModMapSymIsValidHeader(&uBuf.Hdr, cbToRead, cbFile))
                {
                    uBuf.Hdr.achModule[uBuf.Hdr.cchModule] = '\0';

                    /*
                     * Read the whole thing into memory, create an
                     * instance/container and load it with symbols.
                     */
                    void  *pvFile  = NULL;
                    size_t cbFile2 = 0;
                    rc = RTFileReadAllByHandle(hFile, &pvFile, &cbFile2);
                    if (RT_SUCCESS(rc))
                    {
                        RTDBGMOD hCnt;
                        rc = RTDbgModCreate(&hCnt, uBuf.Hdr.achModule, 0 /*cbSeg*/, 0 /*fFlags*/);
                        if (RT_SUCCESS(rc))
                        {
                            rc = rtDbgModMapSymReadIt(hCnt, (uint8_t const *)pvFile, cbFile2);
                            if (RT_SUCCESS(rc))
                                pMod->pvDbgPriv = hCnt;
                            else
                                RTDbgModRelease(hCnt);
                        }
                        RTFileReadAllFree(pvFile, cbFile2);
                    }
                }
                else
                    rc = VERR_DBG_NO_MATCHING_INTERPRETER;
            }
        }
        RTFileClose(hFile);
    }
    Log(("rtDbgModMapSym_TryOpen: %s -> %Rrc, %p\n", pMod->pszDbgFile, rc, pMod->pvDbgPriv));
    return rc;
}



/** Virtual function table for the MAPSYM file reader. */
DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgMapSym =
{
    /*.u32Magic = */            RTDBGMODVTDBG_MAGIC,
    /*.fSupports = */           RT_DBGTYPE_SYM,
    /*.pszName = */             "mapsym",
    /*.pfnTryOpen = */          rtDbgModMapSym_TryOpen,
    /*.pfnClose = */            rtDbgModMapSym_Close,

    /*.pfnRvaToSegOff = */      rtDbgModMapSym_RvaToSegOff,
    /*.pfnImageSize = */        rtDbgModMapSym_ImageSize,

    /*.pfnSegmentAdd = */       rtDbgModMapSym_SegmentAdd,
    /*.pfnSegmentCount = */     rtDbgModMapSym_SegmentCount,
    /*.pfnSegmentByIndex = */   rtDbgModMapSym_SegmentByIndex,

    /*.pfnSymbolAdd = */        rtDbgModMapSym_SymbolAdd,
    /*.pfnSymbolCount = */      rtDbgModMapSym_SymbolCount,
    /*.pfnSymbolByOrdinal = */  rtDbgModMapSym_SymbolByOrdinal,
    /*.pfnSymbolByName = */     rtDbgModMapSym_SymbolByName,
    /*.pfnSymbolByAddr = */     rtDbgModMapSym_SymbolByAddr,

    /*.pfnLineAdd = */          rtDbgModMapSym_LineAdd,
    /*.pfnLineCount = */        rtDbgModMapSym_LineCount,
    /*.pfnLineByOrdinal = */    rtDbgModMapSym_LineByOrdinal,
    /*.pfnLineByAddr = */       rtDbgModMapSym_LineByAddr,

    /*.pfnUnwindFrame = */      rtDbgModMapSym_UnwindFrame,

    /*.u32EndMagic = */         RTDBGMODVTDBG_MAGIC
};