Ignore:
Timestamp:
Aug 20, 2007, 4:43:13 AM (18 years ago)
Author:
bird
Message:

Some refactoring and OS abstraction by instroducing kDbgHlp.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/kDbg/kDbgModPE-generic.cpp

    • Property svn:eol-style set to native
    • Property svn:keywords set to Id
    r3526 r3528  
    1 /* $Id: $ */
     1/* $Id$ */
    22/** @file
    3  *
    4  * kProfile Mark 2 - Debug Info Reader, DbgHelp Base Reader.
    5  *
    6  * Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net.de>
    7  *
     3 * kDbg - The Debug Info Reader, PE Module (Generic).
     4 */
     5
     6/*
     7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
    88 *
    99 * This file is part of kLIBC.
     
    2525 */
    2626
    27 
    2827/*******************************************************************************
    2928*   Header Files                                                               *
    3029*******************************************************************************/
    31 #include <iprt/file.h>
    32 #include <iprt/string.h>
    33 #include <iprt/alloc.h>
    34 #include <iprt/assert.h>
    35 #include <iprt/asm.h>
    36 #include <iprt/err.h>
    37 #include <iprt/thread.h>
    38 #include <iprt/alloca.h>
    39 #include "kDbgBase.h"
    40 #include "DBGInternal.h"
    41 #include "kLdrModPE.h"
    42 
     30#include "kDbg.h"
     31#include "kDbgInternal.h"
     32#include <kLdrModPE.h>
    4333
    4434
     
    4939 * A dbghelp based PE debug reader.
    5040 */
    51 typedef struct RTDBGMODPE
     41typedef struct KDBGMODPE
    5242{
    5343    /** The common module core. */
    54     RTDBGMOD    Core;
     44    KDBGMOD     Core;
    5545    /** The image size. */
    5646    uint32_t    cbImage;
     
    5949    /** The section headers (variable size). The first section is the
    6050     * implicit header section.*/
    61     IMAGE_SECTION_HEADER    aSections[1];
    62 } RTDBGMODPE, *PRTDBGMODPE;
     51    IMAGE_SECTION_HEADER aSections[1];
     52} KDBGMODPE, *PKDBGMODPE;
    6353
    6454
     
    7363 * @param   puRVA       Where to store the RVA on success.
    7464 */
    75 static int rtDbgModPeSegOffToRVA(PRTDBGMODPE pModPe, int32_t iSegment, RTUINTPTR off, uint32_t *puRVA)
     65static int rtDbgModPeSegOffToRVA(PKDBGMODPE pModPe, int32_t iSegment, KDBGADDR off, uint32_t *puRVA)
    7666{
    7767    if (iSegment >= 0)
    7868    {
    79         AssertMsgReturn(iSegment < pModPe->cSections, ("iSegment=%RX32 cSections=%RX32\n", iSegment, pModPe->cSections),
    80                         VERR_DBGMOD_INVALID_ADDRESS);
    81         AssertMsgReturn(off < pModPe->aSections[iSegment].Misc.VirtualSize,
    82                         ("off=%RTptr VirtualSize=%RX32\n", off, pModPe->aSections[iSegment].Misc.VirtualSize),
    83                         VERR_DBGMOD_INVALID_ADDRESS);
     69        kDbgAssertMsgReturn(iSegment < pModPe->cSections, ("iSegment=%x cSections=%x\n", iSegment, pModPe->cSections),
     70                        KDBG_ERR_INVALID_ADDRESS);
     71        kDbgAssertMsgReturn(off < pModPe->aSections[iSegment].Misc.VirtualSize,
     72                        ("off=" PRI_KDBGADDR " VirtualSize=%x\n", off, pModPe->aSections[iSegment].Misc.VirtualSize),
     73                        KDBG_ERR_INVALID_ADDRESS);
    8474        *puRVA = pModPe->aSections[iSegment].VirtualAddress + off;
    85         return VINF_SUCCESS;
    86     }
    87 
    88     if (iSegment == RTDBGSEG_RVA)
    89     {
    90         AssertMsgReturn(off < pModPe->cbImage, ("off=%RTptr, cbImage=%RX32\n", off, pModPe->cbImage),
    91                         VERR_DBGMOD_INVALID_ADDRESS);
     75        return 0;
     76    }
     77
     78    if (iSegment == KDBGSEG_RVA)
     79    {
     80        kDbgAssertMsgReturn(off < pModPe->cbImage, ("off=" PRI_KDBGADDR ", cbImage=%x\n", off, pModPe->cbImage),
     81                            KDBG_ERR_INVALID_ADDRESS);
    9282        *puRVA = off;
    93         return VINF_SUCCESS;
    94     }
    95     AssertMsgFailedReturn(("iSegment=%RI32\n", iSegment), VERR_DBGMOD_INVALID_ADDRESS);
     83        return 0;
     84    }
     85    AssertMsgFailedReturn(("iSegment=%RI32\n", iSegment), KDBG_ERR_INVALID_ADDRESS);
    9686}
    9787
     
    10797 * @param   poff        Where to store the segment offset.
    10898 */
    109 static int rtDbgModPeRVAToSegOff(PRTDBGMODPE pModPe, uint32_t uRVA, int32_t *piSegment, RTUINTPTR *poff)
    110 {
    111     AssertMsgReturn(uRVA < pModPe->cbImage, ("uRVA=%RX32, cbImage=%RX32\n", uRVA, pModPe->cbImage),
    112                     VERR_DBGMOD_INVALID_ADDRESS);
     99static int rtDbgModPeRVAToSegOff(PKDBGMODPE pModPe, uint32_t uRVA, int32_t *piSegment, KDBGADDR *poff)
     100{
     101    kDbgAssertMsgReturn(uRVA < pModPe->cbImage, ("uRVA=%x, cbImage=%x\n", uRVA, pModPe->cbImage),
     102                    KDBG_ERR_INVALID_ADDRESS);
    113103    for (int32_t iSegment = 0; iSegment < pModPe->cSections; iSegment++)
    114104    {
     
    119109            *poff = off;
    120110            *piSegment = iSegment;
    121             return VINF_SUCCESS;
    122         }
    123     }
    124     AssertMsgFailedReturn(("uRVA=%RX32\n", uRVA), VERR_DBGMOD_INVALID_ADDRESS);
    125 }
    126 
    127 
    128 /**
    129  * @copydoc RTDBGMODOPS::pfnQueryLine
    130  */
    131 static DECLCALLBACK(int) rtDbgModPEQueryLine(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGLINE pLine)
    132 {
    133     PRTDBGMODPE pModPe = (PRTDBGMODPE)pMod;
     111            return 0;
     112        }
     113    }
     114    AssertMsgFailedReturn(("uRVA=%x\n", uRVA), KDBG_ERR_INVALID_ADDRESS);
     115}
     116
     117
     118/**
     119 * @copydoc KDBGMODOPS::pfnQueryLine
     120 */
     121static DECLCALLBACK(int) rtDbgModPEQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)
     122{
     123    PKDBGMODPE pModPe = (PKDBGMODPE)pMod;
    134124
    135125    /*
     
    138128    uint32_t uRVA;
    139129    int rc = rtDbgModPeSegOffToRVA(pModPe, iSegment, off, &uRVA);
    140     if (RT_SUCCESS(rc))
     130    if (!rc)
    141131    {
    142132#if 0
     
    146136        if (g_pfnSymGetLineFromAddr64(pModPe->hSymInst, pModPe->ImageBase + uRVA, &off, &Line))
    147137        {
    148             pLine->RVA = (RTUINTPTR)(Line.Address - pModPe->ImageBase);
     138            pLine->RVA = (KDBGADDR)(Line.Address - pModPe->ImageBase);
    149139            rc = rtDbgModPeRVAToSegOff(pModPe, pLine->RVA, &pLine->iSegment, &pLine->offSegment);
    150140            pLine->iLine = Line.LineNumber;
     
    167157
    168158/**
    169  * @copydoc RTDBGMODOPS::pfnQuerySymbol
    170  */
    171 static DECLCALLBACK(int) rtDbgModPEQuerySymbol(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGSYMBOL pSym)
    172 {
    173     PRTDBGMODPE pModPe = (PRTDBGMODPE)pMod;
     159 * @copydoc KDBGMODOPS::pfnQuerySymbol
     160 */
     161static DECLCALLBACK(int) rtDbgModPEQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
     162{
     163    PKDBGMODPE pModPe = (PKDBGMODPE)pMod;
    174164
    175165    /*
     
    178168    uint32_t uRVA;
    179169    int rc = rtDbgModPeSegOffToRVA(pModPe, iSegment, off, &uRVA);
    180     if (RT_SUCCESS(rc))
     170    if (!rc)
    181171    {
    182172#if 0
     
    194184            pSym->fFlags = 0;
    195185            if (Buf.Sym.Flags & SYMFLAG_FUNCTION)
    196                 pSym->fFlags |= RTDBGSYM_FLAGS_CODE;
     186                pSym->fFlags |= KDBGSYM_FLAGS_CODE;
    197187            else if (Buf.Sym.Flags & SYMFLAG_CONSTANT)
    198                 pSym->fFlags |= RTDBGSYM_FLAGS_ABS; /** @todo SYMFLAG_CONSTANT must be tested - documentation is too brief to say what is really meant here.*/
     188                pSym->fFlags |= KDBGSYM_FLAGS_ABS; /** @todo SYMFLAG_CONSTANT must be tested - documentation is too brief to say what is really meant here.*/
    199189            else
    200                 pSym->fFlags |= RTDBGSYM_FLAGS_DATA;
     190                pSym->fFlags |= KDBGSYM_FLAGS_DATA;
    201191            if (Buf.Sym.Flags & SYMFLAG_EXPORT)
    202                 pSym->fFlags |= RTDBGSYM_FLAGS_EXPORTED;
     192                pSym->fFlags |= KDBGSYM_FLAGS_EXPORTED;
    203193            if ((Buf.Sym.Flags & (SYMFLAG_VALUEPRESENT | SYMFLAG_CONSTANT)) == (SYMFLAG_VALUEPRESENT | SYMFLAG_CONSTANT))
    204194            {
    205195                pSym->iSegment   = RTDBGSEG_ABS;
    206                 pSym->offSegment = (RTUINTPTR)Buf.Sym.Value;
    207                 pSym->RVA        = (RTUINTPTR)Buf.Sym.Value;
     196                pSym->offSegment = (KDBGADDR)Buf.Sym.Value;
     197                pSym->RVA        = (KDBGADDR)Buf.Sym.Value;
    208198            }
    209199            else
    210200            {
    211                 pSym->RVA        = (RTUINTPTR)(Buf.Sym.Address - pModPe->ImageBase);
     201                pSym->RVA        = (KDBGADDR)(Buf.Sym.Address - pModPe->ImageBase);
    212202                rc = rtDbgModPeRVAToSegOff(pModPe, pSym->RVA, &pSym->iSegment, &pSym->offSegment);
    213203            }
     
    231221
    232222/**
    233  * @copydoc RTDBGMODOPS::pfnClose
    234  */
    235 static DECLCALLBACK(int) rtDbgModPEClose(PRTDBGMOD pMod)
    236 {
    237     PRTDBGMODPE pModPe = (PRTDBGMODPE)pMod;
     223 * @copydoc KDBGMODOPS::pfnClose
     224 */
     225static DECLCALLBACK(int) rtDbgModPEClose(PKDBGMOD pMod)
     226{
     227    PKDBGMODPE pModPe = (PKDBGMODPE)pMod;
    238228
    239229    //if (g_pfnSymCleanup(pModPe->hSymInst))
    240     //    return VINF_SUCCESS;
     230    //    return 0;
    241231    //
    242232    //DWORD Err = GetLastError();
     
    251241 * Methods for a PE module.
    252242 */
    253 static const RTDBGMODOPS g_rtDbgModPEOps =
     243static const KDBGMODOPS g_rtDbgModPEOps =
    254244{
    255245    "PE (dbghelp)",
     
    271261 *
    272262 */
    273 int rtDbgModPEOpen(RTFILE File, RTFOFF offHdr, const char *pszModulePath, PRTDBGMOD *ppDbgMod)
     263int kdbgModPEOpen(RTFILE File, int64_t offHdr, const char *pszModulePath, PKDBGMOD *ppDbgMod)
    274264{
    275265    /*
     
    277267     */
    278268    IMAGE_FILE_HEADER FHdr;
    279     int rc = RTFileReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, FileHeader), &FHdr, sizeof(FHdr), NULL);
     269    int rc = kDbgHlpReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, FileHeader), &FHdr, sizeof(FHdr), NULL);
    280270    AssertRCReturn(rc, rc);
    281271
    282272    uint32_t cbImage;
    283273    if (FHdr.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32))
    284         rc = RTFileReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader.SizeOfImage),
     274        rc = kDbgHlpReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader.SizeOfImage),
    285275                          &cbImage, sizeof(cbImage), NULL);
    286276    else if (FHdr.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER64))
    287         rc = RTFileReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS64, OptionalHeader.SizeOfImage),
     277        rc = kDbgHlpReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS64, OptionalHeader.SizeOfImage),
    288278                          &cbImage, sizeof(cbImage), NULL);
    289279    else
     
    294284     * Allocate the module and read/construct the section headers.
    295285     */
    296     PRTDBGMODPE pModPe = (PRTDBGMODPE)RTMemAlloc(KDBG_OFFSETOF(RTDBGMODPE, aSections[FHdr.NumberOfSections + 2]));
    297     AssertReturn(pModPe, VERR_NO_MEMORY);
    298     pModPe->Core.u32Magic   = RTDBGMOD_MAGIC;
     286    PKDBGMODPE pModPe = (PKDBGMODPE)RTMemAlloc(KDBG_OFFSETOF(KDBGMODPE, aSections[FHdr.NumberOfSections + 2]));
     287    AssertReturn(pModPe, KDBG_ERR_NO_MEMORY);
     288    pModPe->Core.u32Magic   = KDBGMOD_MAGIC;
    299289    pModPe->Core.pOps       = &g_rtDbgModPEOps;
    300290    pModPe->Core.File       = File;
    301291    pModPe->cbImage         = cbImage;
    302292    pModPe->cSections       = 1 + FHdr.NumberOfSections;
    303     rc = RTFileReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader) + FHdr.SizeOfOptionalHeader,
     293    rc = kDbgHlpReadAt(File, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader) + FHdr.SizeOfOptionalHeader,
    304294                      &pModPe->aSections[1], sizeof(pModPe->aSections[0]) * FHdr.NumberOfSections, NULL);
    305     if (RT_SUCCESS(rc))
     295    if (!rc)
    306296    {
    307297        PIMAGE_SECTION_HEADER pSH = &pModPe->aSections[0];
     
    382372        AssertRC(rc);
    383373
    384     RTMemFree(pModPe);
     374    kDbgHlpFree(pModPe);
    385375    return rc;
    386376}
Note: See TracChangeset for help on using the changeset viewer.