Changeset 3528 for trunk/kDbg/kDbgModPE-generic.cpp
- Timestamp:
- Aug 20, 2007, 4:43:13 AM (18 years ago)
- 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$ */ 2 2 /** @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> 8 8 * 9 9 * This file is part of kLIBC. … … 25 25 */ 26 26 27 28 27 /******************************************************************************* 29 28 * Header Files * 30 29 *******************************************************************************/ 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> 43 33 44 34 … … 49 39 * A dbghelp based PE debug reader. 50 40 */ 51 typedef struct RTDBGMODPE41 typedef struct KDBGMODPE 52 42 { 53 43 /** The common module core. */ 54 RTDBGMODCore;44 KDBGMOD Core; 55 45 /** The image size. */ 56 46 uint32_t cbImage; … … 59 49 /** The section headers (variable size). The first section is the 60 50 * implicit header section.*/ 61 IMAGE_SECTION_HEADER 62 } RTDBGMODPE, *PRTDBGMODPE;51 IMAGE_SECTION_HEADER aSections[1]; 52 } KDBGMODPE, *PKDBGMODPE; 63 53 64 54 … … 73 63 * @param puRVA Where to store the RVA on success. 74 64 */ 75 static int rtDbgModPeSegOffToRVA(P RTDBGMODPE pModPe, int32_t iSegment, RTUINTPTR off, uint32_t *puRVA)65 static int rtDbgModPeSegOffToRVA(PKDBGMODPE pModPe, int32_t iSegment, KDBGADDR off, uint32_t *puRVA) 76 66 { 77 67 if (iSegment >= 0) 78 68 { 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); 84 74 *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); 92 82 *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); 96 86 } 97 87 … … 107 97 * @param poff Where to store the segment offset. 108 98 */ 109 static int rtDbgModPeRVAToSegOff(P RTDBGMODPE 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);99 static 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); 113 103 for (int32_t iSegment = 0; iSegment < pModPe->cSections; iSegment++) 114 104 { … … 119 109 *poff = off; 120 110 *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::pfnQueryLine130 */ 131 static DECLCALLBACK(int) rtDbgModPEQueryLine(P RTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGLINE pLine)132 { 133 P RTDBGMODPE 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 */ 121 static DECLCALLBACK(int) rtDbgModPEQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine) 122 { 123 PKDBGMODPE pModPe = (PKDBGMODPE)pMod; 134 124 135 125 /* … … 138 128 uint32_t uRVA; 139 129 int rc = rtDbgModPeSegOffToRVA(pModPe, iSegment, off, &uRVA); 140 if ( RT_SUCCESS(rc))130 if (!rc) 141 131 { 142 132 #if 0 … … 146 136 if (g_pfnSymGetLineFromAddr64(pModPe->hSymInst, pModPe->ImageBase + uRVA, &off, &Line)) 147 137 { 148 pLine->RVA = ( RTUINTPTR)(Line.Address - pModPe->ImageBase);138 pLine->RVA = (KDBGADDR)(Line.Address - pModPe->ImageBase); 149 139 rc = rtDbgModPeRVAToSegOff(pModPe, pLine->RVA, &pLine->iSegment, &pLine->offSegment); 150 140 pLine->iLine = Line.LineNumber; … … 167 157 168 158 /** 169 * @copydoc RTDBGMODOPS::pfnQuerySymbol170 */ 171 static DECLCALLBACK(int) rtDbgModPEQuerySymbol(P RTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGSYMBOL pSym)172 { 173 P RTDBGMODPE pModPe = (PRTDBGMODPE)pMod;159 * @copydoc KDBGMODOPS::pfnQuerySymbol 160 */ 161 static DECLCALLBACK(int) rtDbgModPEQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym) 162 { 163 PKDBGMODPE pModPe = (PKDBGMODPE)pMod; 174 164 175 165 /* … … 178 168 uint32_t uRVA; 179 169 int rc = rtDbgModPeSegOffToRVA(pModPe, iSegment, off, &uRVA); 180 if ( RT_SUCCESS(rc))170 if (!rc) 181 171 { 182 172 #if 0 … … 194 184 pSym->fFlags = 0; 195 185 if (Buf.Sym.Flags & SYMFLAG_FUNCTION) 196 pSym->fFlags |= RTDBGSYM_FLAGS_CODE;186 pSym->fFlags |= KDBGSYM_FLAGS_CODE; 197 187 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.*/ 199 189 else 200 pSym->fFlags |= RTDBGSYM_FLAGS_DATA;190 pSym->fFlags |= KDBGSYM_FLAGS_DATA; 201 191 if (Buf.Sym.Flags & SYMFLAG_EXPORT) 202 pSym->fFlags |= RTDBGSYM_FLAGS_EXPORTED;192 pSym->fFlags |= KDBGSYM_FLAGS_EXPORTED; 203 193 if ((Buf.Sym.Flags & (SYMFLAG_VALUEPRESENT | SYMFLAG_CONSTANT)) == (SYMFLAG_VALUEPRESENT | SYMFLAG_CONSTANT)) 204 194 { 205 195 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; 208 198 } 209 199 else 210 200 { 211 pSym->RVA = ( RTUINTPTR)(Buf.Sym.Address - pModPe->ImageBase);201 pSym->RVA = (KDBGADDR)(Buf.Sym.Address - pModPe->ImageBase); 212 202 rc = rtDbgModPeRVAToSegOff(pModPe, pSym->RVA, &pSym->iSegment, &pSym->offSegment); 213 203 } … … 231 221 232 222 /** 233 * @copydoc RTDBGMODOPS::pfnClose234 */ 235 static DECLCALLBACK(int) rtDbgModPEClose(P RTDBGMOD pMod)236 { 237 P RTDBGMODPE pModPe = (PRTDBGMODPE)pMod;223 * @copydoc KDBGMODOPS::pfnClose 224 */ 225 static DECLCALLBACK(int) rtDbgModPEClose(PKDBGMOD pMod) 226 { 227 PKDBGMODPE pModPe = (PKDBGMODPE)pMod; 238 228 239 229 //if (g_pfnSymCleanup(pModPe->hSymInst)) 240 // return VINF_SUCCESS;230 // return 0; 241 231 // 242 232 //DWORD Err = GetLastError(); … … 251 241 * Methods for a PE module. 252 242 */ 253 static const RTDBGMODOPS g_rtDbgModPEOps =243 static const KDBGMODOPS g_rtDbgModPEOps = 254 244 { 255 245 "PE (dbghelp)", … … 271 261 * 272 262 */ 273 int rtDbgModPEOpen(RTFILE File, RTFOFF offHdr, const char *pszModulePath, PRTDBGMOD *ppDbgMod)263 int kdbgModPEOpen(RTFILE File, int64_t offHdr, const char *pszModulePath, PKDBGMOD *ppDbgMod) 274 264 { 275 265 /* … … 277 267 */ 278 268 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); 280 270 AssertRCReturn(rc, rc); 281 271 282 272 uint32_t cbImage; 283 273 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), 285 275 &cbImage, sizeof(cbImage), NULL); 286 276 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), 288 278 &cbImage, sizeof(cbImage), NULL); 289 279 else … … 294 284 * Allocate the module and read/construct the section headers. 295 285 */ 296 P RTDBGMODPE 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; 299 289 pModPe->Core.pOps = &g_rtDbgModPEOps; 300 290 pModPe->Core.File = File; 301 291 pModPe->cbImage = cbImage; 302 292 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, 304 294 &pModPe->aSections[1], sizeof(pModPe->aSections[0]) * FHdr.NumberOfSections, NULL); 305 if ( RT_SUCCESS(rc))295 if (!rc) 306 296 { 307 297 PIMAGE_SECTION_HEADER pSH = &pModPe->aSections[0]; … … 382 372 AssertRC(rc); 383 373 384 RTMemFree(pModPe);374 kDbgHlpFree(pModPe); 385 375 return rc; 386 376 } -
Property svn:eol-style
set to
Note:
See TracChangeset
for help on using the changeset viewer.