1 | /* $Id: dbgmodldr.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Debug Module Image Interpretation by RTLdr.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2015 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 | #include <iprt/dbg.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/ldr.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/param.h>
|
---|
40 | #include <iprt/path.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include "internal/dbgmod.h"
|
---|
43 | #include "internal/ldr.h"
|
---|
44 | #include "internal/magics.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Structures and Typedefs *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | /**
|
---|
51 | * The instance data of the RTLdr based image reader.
|
---|
52 | */
|
---|
53 | typedef struct RTDBGMODLDR
|
---|
54 | {
|
---|
55 | /** The loader handle. */
|
---|
56 | RTLDRMOD hLdrMod;
|
---|
57 | } RTDBGMODLDR;
|
---|
58 | /** Pointer to instance data NM map reader. */
|
---|
59 | typedef RTDBGMODLDR *PRTDBGMODLDR;
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | /** @interface_method_impl{RTDBGMODVTIMG,pfnQueryProp} */
|
---|
64 | static DECLCALLBACK(int) rtDbgModLdr_QueryProp(PRTDBGMODINT pMod, RTLDRPROP enmProp, void *pvBuf, size_t cbBuf)
|
---|
65 | {
|
---|
66 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
67 | return RTLdrQueryProp(pThis->hLdrMod, enmProp, pvBuf, cbBuf);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /** @interface_method_impl{RTDBGMODVTIMG,pfnGetArch} */
|
---|
72 | static DECLCALLBACK(RTLDRARCH) rtDbgModLdr_GetArch(PRTDBGMODINT pMod)
|
---|
73 | {
|
---|
74 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
75 | return RTLdrGetArch(pThis->hLdrMod);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /** @interface_method_impl{RTDBGMODVTIMG,pfnGetFormat} */
|
---|
80 | static DECLCALLBACK(RTLDRFMT) rtDbgModLdr_GetFormat(PRTDBGMODINT pMod)
|
---|
81 | {
|
---|
82 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
83 | return RTLdrGetFormat(pThis->hLdrMod);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /** @interface_method_impl{RTDBGMODVTIMG,pfnReadAt} */
|
---|
88 | static DECLCALLBACK(int) rtDbgModLdr_ReadAt(PRTDBGMODINT pMod, uint32_t iDbgInfoHint, RTFOFF off, void *pvBuf, size_t cb)
|
---|
89 | {
|
---|
90 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
91 | return rtLdrReadAt(pThis->hLdrMod, pvBuf, UINT32_MAX /** @todo iDbgInfo*/, off, cb);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /** @interface_method_impl{RTDBGMODVTIMG,pfnUnmapPart} */
|
---|
96 | static DECLCALLBACK(int) rtDbgModLdr_UnmapPart(PRTDBGMODINT pMod, size_t cb, void const **ppvMap)
|
---|
97 | {
|
---|
98 | NOREF(pMod); NOREF(cb);
|
---|
99 | RTMemFree((void *)*ppvMap);
|
---|
100 | *ppvMap = NULL;
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /** @interface_method_impl{RTDBGMODVTIMG,pfnMapPart} */
|
---|
106 | static DECLCALLBACK(int) rtDbgModLdr_MapPart(PRTDBGMODINT pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void const **ppvMap)
|
---|
107 | {
|
---|
108 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
109 |
|
---|
110 | void *pvMap = RTMemAlloc(cb);
|
---|
111 | if (!pvMap)
|
---|
112 | return VERR_NO_MEMORY;
|
---|
113 |
|
---|
114 | int rc = rtLdrReadAt(pThis->hLdrMod, pvMap, iDbgInfo, off, cb);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | *ppvMap = pvMap;
|
---|
117 | else
|
---|
118 | {
|
---|
119 | RTMemFree(pvMap);
|
---|
120 | *ppvMap = NULL;
|
---|
121 | }
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /** @interface_method_impl{RTDBGMODVTIMG,pfnGetLoadedSize} */
|
---|
127 | static DECLCALLBACK(RTUINTPTR) rtDbgModLdr_GetLoadedSize(PRTDBGMODINT pMod)
|
---|
128 | {
|
---|
129 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
130 | return RTLdrSize(pThis->hLdrMod);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | /** @interface_method_impl{RTDBGMODVTIMG,pfnRvaToSegOffset} */
|
---|
135 | static DECLCALLBACK(int) rtDbgModLdr_RvaToSegOffset(PRTDBGMODINT pMod, RTLDRADDR uRva,
|
---|
136 | PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
|
---|
137 | {
|
---|
138 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
139 | return RTLdrRvaToSegOffset(pThis->hLdrMod, uRva, piSeg, poffSeg);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /** @interface_method_impl{RTDBGMODVTIMG,pfnLinkAddressToSegOffset} */
|
---|
144 | static DECLCALLBACK(int) rtDbgModLdr_LinkAddressToSegOffset(PRTDBGMODINT pMod, RTLDRADDR LinkAddress,
|
---|
145 | PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
|
---|
146 | {
|
---|
147 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
148 | return RTLdrLinkAddressToSegOffset(pThis->hLdrMod, LinkAddress, piSeg, poffSeg);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | /** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
|
---|
153 | static DECLCALLBACK(int) rtDbgModLdr_EnumSymbols(PRTDBGMODINT pMod, uint32_t fFlags, RTLDRADDR BaseAddress,
|
---|
154 | PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
|
---|
155 | {
|
---|
156 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
157 | return RTLdrEnumSymbols(pThis->hLdrMod, fFlags, NULL /*pvBits*/, BaseAddress, pfnCallback, pvUser);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
|
---|
162 | static DECLCALLBACK(int) rtDbgModLdr_EnumSegments(PRTDBGMODINT pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
|
---|
163 | {
|
---|
164 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
165 | return RTLdrEnumSegments(pThis->hLdrMod, pfnCallback, pvUser);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /** @interface_method_impl{RTDBGMODVTIMG,pfnEnumDbgInfo} */
|
---|
170 | static DECLCALLBACK(int) rtDbgModLdr_EnumDbgInfo(PRTDBGMODINT pMod, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
|
---|
171 | {
|
---|
172 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
173 | return RTLdrEnumDbgInfo(pThis->hLdrMod, NULL, pfnCallback, pvUser);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /** @interface_method_impl{RTDBGMODVTIMG,pfnClose} */
|
---|
178 | static DECLCALLBACK(int) rtDbgModLdr_Close(PRTDBGMODINT pMod)
|
---|
179 | {
|
---|
180 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
181 | AssertPtr(pThis);
|
---|
182 |
|
---|
183 | int rc = RTLdrClose(pThis->hLdrMod); AssertRC(rc);
|
---|
184 | pThis->hLdrMod = NIL_RTLDRMOD;
|
---|
185 |
|
---|
186 | RTMemFree(pThis);
|
---|
187 |
|
---|
188 | return VINF_SUCCESS;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /** @interface_method_impl{RTDBGMODVTIMG,pfnTryOpen} */
|
---|
193 | static DECLCALLBACK(int) rtDbgModLdr_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
|
---|
194 | {
|
---|
195 | RTLDRMOD hLdrMod;
|
---|
196 | int rc = RTLdrOpen(pMod->pszImgFile, RTLDR_O_FOR_DEBUG, enmArch, &hLdrMod);
|
---|
197 | if (RT_SUCCESS(rc))
|
---|
198 | {
|
---|
199 | rc = rtDbgModLdrOpenFromHandle(pMod, hLdrMod);
|
---|
200 | if (RT_FAILURE(rc))
|
---|
201 | RTLdrClose(hLdrMod);
|
---|
202 | }
|
---|
203 | return rc;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /** Virtual function table for the RTLdr based image reader. */
|
---|
208 | DECL_HIDDEN_CONST(RTDBGMODVTIMG) const g_rtDbgModVtImgLdr =
|
---|
209 | {
|
---|
210 | /*.u32Magic = */ RTDBGMODVTIMG_MAGIC,
|
---|
211 | /*.fReserved = */ 0,
|
---|
212 | /*.pszName = */ "RTLdr",
|
---|
213 | /*.pfnTryOpen = */ rtDbgModLdr_TryOpen,
|
---|
214 | /*.pfnClose = */ rtDbgModLdr_Close,
|
---|
215 | /*.pfnEnumDbgInfo = */ rtDbgModLdr_EnumDbgInfo,
|
---|
216 | /*.pfnEnumSegments = */ rtDbgModLdr_EnumSegments,
|
---|
217 | /*.pfnEnumSymbols = */ rtDbgModLdr_EnumSymbols,
|
---|
218 | /*.pfnGetLoadedSize = */ rtDbgModLdr_GetLoadedSize,
|
---|
219 | /*.pfnLinkAddressToSegOffset = */ rtDbgModLdr_LinkAddressToSegOffset,
|
---|
220 | /*.pfnRvaToSegOffset= */ rtDbgModLdr_RvaToSegOffset,
|
---|
221 | /*.pfnMapPart = */ rtDbgModLdr_MapPart,
|
---|
222 | /*.pfnUnmapPart = */ rtDbgModLdr_UnmapPart,
|
---|
223 | /*.pfnReadAt = */ rtDbgModLdr_ReadAt,
|
---|
224 | /*.pfnGetFormat = */ rtDbgModLdr_GetFormat,
|
---|
225 | /*.pfnGetArch = */ rtDbgModLdr_GetArch,
|
---|
226 | /*.pfnQueryProp = */ rtDbgModLdr_QueryProp,
|
---|
227 |
|
---|
228 | /*.u32EndMagic = */ RTDBGMODVTIMG_MAGIC
|
---|
229 | };
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Open PE-image trick.
|
---|
234 | *
|
---|
235 | * @returns IPRT status code
|
---|
236 | * @param pDbgMod The debug module instance.
|
---|
237 | * @param hLdrMod The module to open a image debug backend for.
|
---|
238 | */
|
---|
239 | DECLHIDDEN(int) rtDbgModLdrOpenFromHandle(PRTDBGMODINT pDbgMod, RTLDRMOD hLdrMod)
|
---|
240 | {
|
---|
241 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)RTMemAllocZ(sizeof(RTDBGMODLDR));
|
---|
242 | if (!pThis)
|
---|
243 | return VERR_NO_MEMORY;
|
---|
244 |
|
---|
245 | pThis->hLdrMod = hLdrMod;
|
---|
246 | pDbgMod->pvImgPriv = pThis;
|
---|
247 | return VINF_SUCCESS;
|
---|
248 | }
|
---|
249 |
|
---|