/* $Id: $ */ /** @file * * kProfile Mark 2 - Debug Info Reader, DbgHelp Base Reader. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLIBC. * * kLIBC is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLIBC is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLIBC; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include #include #include #include #include #include #include #include "DBGInternal.h" #include "internal/ldrPE.h" /******************************************************************************* * Structures and Typedefs * *******************************************************************************/ /** * A dbghelp based PE debug reader. */ typedef struct RTDBGMODPE { /** The common module core. */ RTDBGMOD Core; /** The image size. */ uint32_t cbImage; /** The number of sections. (We've added the implicit header section.) */ int32_t cSections; /** The section headers (variable size). The first section is the * implicit header section.*/ IMAGE_SECTION_HEADER aSections[1]; } RTDBGMODPE, *PRTDBGMODPE; /** * Calcs the RVA for a segment:offset address. * * @returns IPRT status code. * * @param pModPe The PE debug module instance. * @param iSegment The segment number. Special segments are dealt with as well. * @param off The segment offset. * @param puRVA Where to store the RVA on success. */ static int rtDbgModPeSegOffToRVA(PRTDBGMODPE pModPe, int32_t iSegment, RTUINTPTR off, uint32_t *puRVA) { if (iSegment >= 0) { AssertMsgReturn(iSegment < pModPe->cSections, ("iSegment=%RX32 cSections=%RX32\n", iSegment, pModPe->cSections), VERR_DBGMOD_INVALID_ADDRESS); AssertMsgReturn(off < pModPe->aSections[iSegment].Misc.VirtualSize, ("off=%RTptr VirtualSize=%RX32\n", off, pModPe->aSections[iSegment].Misc.VirtualSize), VERR_DBGMOD_INVALID_ADDRESS); *puRVA = pModPe->aSections[iSegment].VirtualAddress + off; return VINF_SUCCESS; } if (iSegment == RTDBGSEG_RVA) { AssertMsgReturn(off < pModPe->cbImage, ("off=%RTptr, cbImage=%RX32\n", off, pModPe->cbImage), VERR_DBGMOD_INVALID_ADDRESS); *puRVA = off; return VINF_SUCCESS; } AssertMsgFailedReturn(("iSegment=%RI32\n", iSegment), VERR_DBGMOD_INVALID_ADDRESS); } /** * Calcs the segment:offset address for a RVA. * * @returns IPRT status code. * * @param pModPe The PE debug module instance. * @param uRVA The RVA. * @param piSegment Where to store the segment number. * @param poff Where to store the segment offset. */ static int rtDbgModPeRVAToSegOff(PRTDBGMODPE pModPe, uint32_t uRVA, int32_t *piSegment, RTUINTPTR *poff) { AssertMsgReturn(uRVA < pModPe->cbImage, ("uRVA=%RX32, cbImage=%RX32\n", uRVA, pModPe->cbImage), VERR_DBGMOD_INVALID_ADDRESS); for (int32_t iSegment = 0; iSegment < pModPe->cSections; iSegment++) { /** @todo should probably be less strict about address in the alignment gaps. */ uint32_t off = uRVA - pModPe->aSections[iSegment].VirtualAddress; if (off < pModPe->aSections[iSegment].Misc.VirtualSize) { *poff = off; *piSegment = iSegment; return VINF_SUCCESS; } } AssertMsgFailedReturn(("uRVA=%RX32\n", uRVA), VERR_DBGMOD_INVALID_ADDRESS); } /** * @copydoc RTDBGMODOPS::pfnQueryLine */ static DECLCALLBACK(int) rtDbgModPEQueryLine(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGLINE pLine) { PRTDBGMODPE pModPe = (PRTDBGMODPE)pMod; /* * Translate the address to an RVA. */ uint32_t uRVA; int rc = rtDbgModPeSegOffToRVA(pModPe, iSegment, off, &uRVA); if (RT_SUCCESS(rc)) { #if 0 DWORD64 off; IMAGEHLP_LINE64 Line; Line.SizeOfStruct = sizeof(Line); if (g_pfnSymGetLineFromAddr64(pModPe->hSymInst, pModPe->ImageBase + uRVA, &off, &Line)) { pLine->RVA = (RTUINTPTR)(Line.Address - pModPe->ImageBase); rc = rtDbgModPeRVAToSegOff(pModPe, pLine->RVA, &pLine->iSegment, &pLine->offSegment); pLine->iLine = Line.LineNumber; pLine->cchFile = strlen(Line.FileName); if (pLine->cchFile >= sizeof(pLine->szFile)) pLine->cchFile = sizeof(pLine->szFile) - 1; memcpy(pLine->szFile, Line.FileName, pLine->cchFile + 1); } else { DWORD Err = GetLastError(); rc = RTErrConvertFromWin32(Err); } #endif rc = VERR_NOT_IMPLEMENTED; } return rc; } /** * @copydoc RTDBGMODOPS::pfnQuerySymbol */ static DECLCALLBACK(int) rtDbgModPEQuerySymbol(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGSYMBOL pSym) { PRTDBGMODPE pModPe = (PRTDBGMODPE)pMod; /* * Translate the address to an RVA. */ uint32_t uRVA; int rc = rtDbgModPeSegOffToRVA(pModPe, iSegment, off, &uRVA); if (RT_SUCCESS(rc)) { #if 0 DWORD64 off; union { SYMBOL_INFO Sym; char achBuffer[sizeof(SYMBOL_INFO) + RTDBG_SYMBOL_MAX]; } Buf; Buf.Sym.SizeOfStruct = sizeof(SYMBOL_INFO); Buf.Sym.MaxNameLen = RTDBG_SYMBOL_MAX; if (g_pfnSymFromAddr(pModPe->hSymInst, pModPe->ImageBase + uRVA, &off, &Buf.Sym)) { pSym->cb = Buf.Sym.Size; pSym->fFlags = 0; if (Buf.Sym.Flags & SYMFLAG_FUNCTION) pSym->fFlags |= RTDBGSYM_FLAGS_CODE; else if (Buf.Sym.Flags & SYMFLAG_CONSTANT) pSym->fFlags |= RTDBGSYM_FLAGS_ABS; /** @todo SYMFLAG_CONSTANT must be tested - documentation is too brief to say what is really meant here.*/ else pSym->fFlags |= RTDBGSYM_FLAGS_DATA; if (Buf.Sym.Flags & SYMFLAG_EXPORT) pSym->fFlags |= RTDBGSYM_FLAGS_EXPORTED; if ((Buf.Sym.Flags & (SYMFLAG_VALUEPRESENT | SYMFLAG_CONSTANT)) == (SYMFLAG_VALUEPRESENT | SYMFLAG_CONSTANT)) { pSym->iSegment = RTDBGSEG_ABS; pSym->offSegment = (RTUINTPTR)Buf.Sym.Value; pSym->RVA = (RTUINTPTR)Buf.Sym.Value; } else { pSym->RVA = (RTUINTPTR)(Buf.Sym.Address - pModPe->ImageBase); rc = rtDbgModPeRVAToSegOff(pModPe, pSym->RVA, &pSym->iSegment, &pSym->offSegment); } pSym->cchName = (uint16_t)Buf.Sym.NameLen; if (pSym->cchName >= sizeof(pSym->szName)) pSym->cchName = sizeof(pSym->szName) - 1; memcpy(pSym->szName, Buf.Sym.Name, Buf.Sym.NameLen); pSym->szName[Buf.Sym.NameLen] = '\0'; } else { DWORD Err = GetLastError(); rc = RTErrConvertFromWin32(Err); } #endif rc = VERR_NOT_IMPLEMENTED; } return rc; } /** * @copydoc RTDBGMODOPS::pfnClose */ static DECLCALLBACK(int) rtDbgModPEClose(PRTDBGMOD pMod) { PRTDBGMODPE pModPe = (PRTDBGMODPE)pMod; //if (g_pfnSymCleanup(pModPe->hSymInst)) // return VINF_SUCCESS; // //DWORD Err = GetLastError(); //int rc = RTErrConvertFromWin32(Err); //AssertMsgFailed(("SymInitialize failed: Err=%d rc=%Rrc\n", Err, rc)); //return rc; return VERR_NOT_IMPLEMENTED; } /** * Methods for a PE module. */ static const RTDBGMODOPS g_rtDbgModPEOps = { "PE (dbghelp)", rtDbgModPEClose, rtDbgModPEQuerySymbol, rtDbgModPEQueryLine }; /** * Opens the debug info for a PE image using the windows dbghelp library. * * @returns IPRT status code. * * @param File The handle to the module. * @param offHdr The offset of the PE header. * @param pszModulePath The path to the module. * @param ppDbgMod Where to store the module handle. * */ int rtDbgModPEOpen(RTFILE File, RTFOFF offHdr, const char *pszModulePath, PRTDBGMOD *ppDbgMod) { /* * We need to read the section headers and get the image size. */ IMAGE_FILE_HEADER FHdr; int rc = RTFileReadAt(File, offHdr + OFFSETOF(IMAGE_NT_HEADERS32, FileHeader), &FHdr, sizeof(FHdr), NULL); AssertRCReturn(rc, rc); uint32_t cbImage; if (FHdr.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER32)) rc = RTFileReadAt(File, offHdr + OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader.SizeOfImage), &cbImage, sizeof(cbImage), NULL); else if (FHdr.SizeOfOptionalHeader == sizeof(IMAGE_OPTIONAL_HEADER64)) rc = RTFileReadAt(File, offHdr + OFFSETOF(IMAGE_NT_HEADERS64, OptionalHeader.SizeOfImage), &cbImage, sizeof(cbImage), NULL); else AssertFailedReturn(VERR_BAD_EXE_FORMAT); AssertRCReturn(rc, rc); /* * Allocate the module and read/construct the section headers. */ PRTDBGMODPE pModPe = (PRTDBGMODPE)RTMemAlloc(OFFSETOF(RTDBGMODPE, aSections[FHdr.NumberOfSections + 2])); AssertReturn(pModPe, VERR_NO_MEMORY); pModPe->Core.u32Magic = RTDBGMOD_MAGIC; pModPe->Core.pOps = &g_rtDbgModPEOps; pModPe->Core.File = File; pModPe->cbImage = cbImage; pModPe->cSections = 1 + FHdr.NumberOfSections; rc = RTFileReadAt(File, offHdr + OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader) + FHdr.SizeOfOptionalHeader, &pModPe->aSections[1], sizeof(pModPe->aSections[0]) * FHdr.NumberOfSections, NULL); if (RT_SUCCESS(rc)) { PIMAGE_SECTION_HEADER pSH = &pModPe->aSections[0]; memcpy(pSH->Name, "headers", sizeof(pSH->Name)); pSH->Misc.VirtualSize = pModPe->aSections[1].VirtualAddress; pSH->VirtualAddress = 0; pSH->SizeOfRawData = pSH->Misc.VirtualSize; pSH->PointerToRawData = 0; pSH->PointerToRelocations = 0; pSH->PointerToLinenumbers = 0; pSH->NumberOfRelocations = 0; pSH->NumberOfLinenumbers = 0; pSH->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_MEM_READ; uint32_t uTheEnd = pModPe->aSections[FHdr.NumberOfSections].VirtualAddress + pModPe->aSections[FHdr.NumberOfSections].Misc.VirtualSize; if (uTheEnd < cbImage) { pSH = &pModPe->aSections[pModPe->cSections++]; memcpy(pSH->Name, "tail\0\0\0", sizeof(pSH->Name)); pSH->Misc.VirtualSize = cbImage - uTheEnd; pSH->VirtualAddress = uTheEnd; pSH->SizeOfRawData = pSH->Misc.VirtualSize; pSH->PointerToRawData = 0; pSH->PointerToRelocations = 0; pSH->PointerToLinenumbers = 0; pSH->NumberOfRelocations = 0; pSH->NumberOfLinenumbers = 0; pSH->Characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_ALIGN_1BYTES | IMAGE_SCN_MEM_READ; } #if 0 /* * Find a new dbghelp handle. * * We assume 4GB of handles outlast most debugging sessions, or in anyways that * when we start reusing handles they are no longer in use. :-) */ static volatile uint32_t s_u32LastHandle = 1; HANDLE hSymInst = (HANDLE)ASMAtomicIncU32(&s_u32LastHandle); while ( hSymInst == INVALID_HANDLE_VALUE || hSymInst == (HANDLE)0 || hSymInst == GetCurrentProcess()) hSymInst = (HANDLE)ASMAtomicIncU32(&s_u32LastHandle); /* * Initialize dbghelp and try open the specified module. */ if (g_pfnSymInitialize(hSymInst, NULL, FALSE)) { g_pfnSymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_AUTO_PUBLICS | SYMOPT_ALLOW_ABSOLUTE_SYMBOLS); RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL); /* don't know if this is required or not... */ DWORD64 ImageBase = g_pfnSymLoadModule64(hSymInst, (HANDLE)File, pszModulePath, NULL, 0x00400000, 0); if (ImageBase) { pModPe->hSymInst = hSymInst; pModPe->ImageBase = ImageBase; *ppDbgMod = &pModPe->Core; return rc; } DWORD Err = GetLastError(); rc = RTErrConvertFromWin32(Err); AssertMsgFailed(("SymLoadModule64 failed: Err=%d rc=%Rrc\n", Err, rc)); g_pfnSymCleanup(hSymInst); } else { DWORD Err = GetLastError(); rc = RTErrConvertFromWin32(Err); AssertMsgFailed(("SymInitialize failed: Err=%d rc=%Rrc\n", Err, rc)); } #endif rc = VERR_NOT_IMPLEMENTED; } else AssertRC(rc); RTMemFree(pModPe); return rc; }