1 | /* $Id: dbgmodexports.cpp 57974 2015-09-30 18:27:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Debug Module Using Image Exports.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-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 | #define LOG_GROUP RTLOGGROUP_DBG
|
---|
32 | #include <iprt/dbg.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/alloca.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/log.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include "internal/dbgmod.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | typedef struct RTDBGMODEXPORTARGS
|
---|
47 | {
|
---|
48 | PRTDBGMODINT pDbgMod;
|
---|
49 | RTLDRADDR uImageBase;
|
---|
50 | RTLDRADDR uRvaNext;
|
---|
51 | uint32_t cSegs;
|
---|
52 | } RTDBGMODEXPORTARGS;
|
---|
53 | /** Pointer to an argument package. */
|
---|
54 | typedef RTDBGMODEXPORTARGS *PRTDBGMODEXPORTARGS;
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * @callback_method_impl{FNRTLDRENUMSYMS,
|
---|
59 | * Copies the symbols over into the container} */
|
---|
60 | static DECLCALLBACK(int) rtDbgModExportsAddSymbolCallback(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol,
|
---|
61 | RTLDRADDR Value, void *pvUser)
|
---|
62 | {
|
---|
63 | PRTDBGMODEXPORTARGS pArgs = (PRTDBGMODEXPORTARGS)pvUser;
|
---|
64 | NOREF(hLdrMod);
|
---|
65 |
|
---|
66 | if (Value >= pArgs->uImageBase)
|
---|
67 | {
|
---|
68 | int rc = RTDbgModSymbolAdd(pArgs->pDbgMod, pszSymbol, RTDBGSEGIDX_RVA, Value - pArgs->uImageBase,
|
---|
69 | 0 /*cb*/, 0 /* fFlags */, NULL /*piOrdinal*/);
|
---|
70 | Log(("Symbol #%05u %#018RTptr %s [%Rrc]\n", uSymbol, Value, pszSymbol, rc)); NOREF(rc);
|
---|
71 | }
|
---|
72 | else
|
---|
73 | Log(("Symbol #%05u %#018RTptr %s [SKIPPED - INVALID ADDRESS]\n", uSymbol, Value, pszSymbol));
|
---|
74 | return VINF_SUCCESS;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /** @callback_method_impl{FNRTLDRENUMSEGS,
|
---|
79 | * Copies the segments over into the container} */
|
---|
80 | static DECLCALLBACK(int) rtDbgModExportsAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
|
---|
81 | {
|
---|
82 | PRTDBGMODEXPORTARGS pArgs = (PRTDBGMODEXPORTARGS)pvUser;
|
---|
83 | Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
|
---|
84 | pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
|
---|
85 | NOREF(hLdrMod);
|
---|
86 |
|
---|
87 | pArgs->cSegs++;
|
---|
88 |
|
---|
89 | /* Add dummy segments for segments that doesn't get mapped. */
|
---|
90 | if ( pSeg->LinkAddress == NIL_RTLDRADDR
|
---|
91 | || pSeg->RVA == NIL_RTLDRADDR)
|
---|
92 | return RTDbgModSegmentAdd(pArgs->pDbgMod, 0, 0, pSeg->pszName, 0 /*fFlags*/, NULL);
|
---|
93 |
|
---|
94 | RTLDRADDR uRva = pSeg->RVA;
|
---|
95 | #if 0 /* Kluge for the .data..percpu segment in 64-bit linux kernels and similar. (Moved to ELF.) */
|
---|
96 | if (uRva < pArgs->uRvaNext)
|
---|
97 | uRva = RT_ALIGN_T(pArgs->uRvaNext, pSeg->Alignment, RTLDRADDR);
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | /* Find the best base address for the module. */
|
---|
101 | if ( ( !pArgs->uImageBase
|
---|
102 | || pArgs->uImageBase > pSeg->LinkAddress)
|
---|
103 | && ( pSeg->LinkAddress != 0 /* .data..percpu again. */
|
---|
104 | || pArgs->cSegs == 1))
|
---|
105 | pArgs->uImageBase = pSeg->LinkAddress;
|
---|
106 |
|
---|
107 | /* Add it. */
|
---|
108 | RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
|
---|
109 | pArgs->uRvaNext = uRva + cb;
|
---|
110 | return RTDbgModSegmentAdd(pArgs->pDbgMod, uRva, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Creates the debug info side of affairs based on exports and segments found in
|
---|
116 | * the image part.
|
---|
117 | *
|
---|
118 | * The image part must be successfully initialized prior to the call, while the
|
---|
119 | * debug bits must not be present of course.
|
---|
120 | *
|
---|
121 | * @returns IPRT status code
|
---|
122 | * @param pDbgMod The debug module structure.
|
---|
123 | */
|
---|
124 | DECLHIDDEN(int) rtDbgModCreateForExports(PRTDBGMODINT pDbgMod)
|
---|
125 | {
|
---|
126 | AssertReturn(!pDbgMod->pDbgVt, VERR_DBG_MOD_IPE);
|
---|
127 | AssertReturn(pDbgMod->pImgVt, VERR_DBG_MOD_IPE);
|
---|
128 | RTUINTPTR cbImage = pDbgMod->pImgVt->pfnImageSize(pDbgMod);
|
---|
129 | AssertReturn(cbImage > 0, VERR_DBG_MOD_IPE);
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * We simply use a container type for this work.
|
---|
133 | */
|
---|
134 | int rc = rtDbgModContainerCreate(pDbgMod, 0);
|
---|
135 | if (RT_FAILURE(rc))
|
---|
136 | return rc;
|
---|
137 | pDbgMod->fExports = true;
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Copy the segments and symbols.
|
---|
141 | */
|
---|
142 | RTDBGMODEXPORTARGS Args;
|
---|
143 | Args.pDbgMod = pDbgMod;
|
---|
144 | Args.uImageBase = 0;
|
---|
145 | Args.uRvaNext = 0;
|
---|
146 | Args.cSegs = 0;
|
---|
147 | rc = pDbgMod->pImgVt->pfnEnumSegments(pDbgMod, rtDbgModExportsAddSegmentsCallback, &Args);
|
---|
148 | if (RT_SUCCESS(rc))
|
---|
149 | {
|
---|
150 | rc = pDbgMod->pImgVt->pfnEnumSymbols(pDbgMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL | RTLDR_ENUM_SYMBOL_FLAGS_NO_FWD,
|
---|
151 | Args.uImageBase ? Args.uImageBase : 0x10000,
|
---|
152 | rtDbgModExportsAddSymbolCallback, &Args);
|
---|
153 | if (RT_FAILURE(rc))
|
---|
154 | Log(("rtDbgModCreateForExports: Error during symbol enum: %Rrc\n", rc));
|
---|
155 | }
|
---|
156 | else
|
---|
157 | Log(("rtDbgModCreateForExports: Error during segment enum: %Rrc\n", rc));
|
---|
158 |
|
---|
159 | /* This won't fail. */
|
---|
160 | if (RT_SUCCESS(rc))
|
---|
161 | rc = VINF_SUCCESS;
|
---|
162 | else
|
---|
163 | rc = -rc; /* Change it into a warning. */
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|