VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmodexports.cpp@ 76408

Last change on this file since 76408 was 73760, checked in by vboxsync, 6 years ago

IPRT/dbgmodexports: paranoia.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: dbgmodexports.cpp 73760 2018-08-19 13:41:26Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Using Image Exports.
4 */
5
6/*
7 * Copyright (C) 2013-2017 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*********************************************************************************************************************************/
46typedef struct RTDBGMODEXPORTARGS
47{
48 PRTDBGMODINT pDbgMod;
49 RTLDRADDR uImageBase;
50 RTLDRADDR uRvaNext;
51 uint32_t cSegs;
52} RTDBGMODEXPORTARGS;
53/** Pointer to an argument package. */
54typedef RTDBGMODEXPORTARGS *PRTDBGMODEXPORTARGS;
55
56
57/**
58 * @callback_method_impl{FNRTLDRENUMSYMS,
59 * Copies the symbols over into the container} */
60static 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 char szOrdinalNm[48];
69 if (!pszSymbol || *pszSymbol == '\0')
70 {
71 RTStrPrintf(szOrdinalNm, sizeof(szOrdinalNm), "Ordinal%u", uSymbol);
72 pszSymbol = szOrdinalNm;
73 }
74
75 int rc = RTDbgModSymbolAdd(pArgs->pDbgMod, pszSymbol, RTDBGSEGIDX_RVA, Value - pArgs->uImageBase,
76 0 /*cb*/, 0 /* fFlags */, NULL /*piOrdinal*/);
77 Log(("Symbol #%05u %#018RTptr %s [%Rrc]\n", uSymbol, Value, pszSymbol, rc)); NOREF(rc);
78 }
79 else
80 Log(("Symbol #%05u %#018RTptr %s [SKIPPED - INVALID ADDRESS]\n", uSymbol, Value, pszSymbol));
81 return VINF_SUCCESS;
82}
83
84
85/** @callback_method_impl{FNRTLDRENUMSEGS,
86 * Copies the segments over into the container} */
87static DECLCALLBACK(int) rtDbgModExportsAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
88{
89 PRTDBGMODEXPORTARGS pArgs = (PRTDBGMODEXPORTARGS)pvUser;
90 Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
91 pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
92 NOREF(hLdrMod);
93
94 pArgs->cSegs++;
95
96 /* Add dummy segments for segments that doesn't get mapped. */
97 if ( pSeg->LinkAddress == NIL_RTLDRADDR
98 || pSeg->RVA == NIL_RTLDRADDR)
99 return RTDbgModSegmentAdd(pArgs->pDbgMod, 0, 0, pSeg->pszName, 0 /*fFlags*/, NULL);
100
101 RTLDRADDR uRva = pSeg->RVA;
102#if 0 /* Kluge for the .data..percpu segment in 64-bit linux kernels and similar. (Moved to ELF.) */
103 if (uRva < pArgs->uRvaNext)
104 uRva = RT_ALIGN_T(pArgs->uRvaNext, pSeg->Alignment, RTLDRADDR);
105#endif
106
107 /* Find the best base address for the module. */
108 if ( ( !pArgs->uImageBase
109 || pArgs->uImageBase > pSeg->LinkAddress)
110 && ( pSeg->LinkAddress != 0 /* .data..percpu again. */
111 || pArgs->cSegs == 1))
112 pArgs->uImageBase = pSeg->LinkAddress;
113
114 /* Add it. */
115 RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
116 pArgs->uRvaNext = uRva + cb;
117 return RTDbgModSegmentAdd(pArgs->pDbgMod, uRva, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
118}
119
120
121/**
122 * Creates the debug info side of affairs based on exports and segments found in
123 * the image part.
124 *
125 * The image part must be successfully initialized prior to the call, while the
126 * debug bits must not be present of course.
127 *
128 * @returns IPRT status code
129 * @param pDbgMod The debug module structure.
130 */
131DECLHIDDEN(int) rtDbgModCreateForExports(PRTDBGMODINT pDbgMod)
132{
133 AssertReturn(!pDbgMod->pDbgVt, VERR_DBG_MOD_IPE);
134 AssertReturn(pDbgMod->pImgVt, VERR_DBG_MOD_IPE);
135 RTUINTPTR cbImage = pDbgMod->pImgVt->pfnImageSize(pDbgMod);
136 AssertReturn(cbImage > 0, VERR_DBG_MOD_IPE);
137
138 /*
139 * We simply use a container type for this work.
140 */
141 int rc = rtDbgModContainerCreate(pDbgMod, 0);
142 if (RT_FAILURE(rc))
143 return rc;
144 pDbgMod->fExports = true;
145
146 /*
147 * Copy the segments and symbols.
148 */
149 RTDBGMODEXPORTARGS Args;
150 Args.pDbgMod = pDbgMod;
151 Args.uImageBase = 0;
152 Args.uRvaNext = 0;
153 Args.cSegs = 0;
154 rc = pDbgMod->pImgVt->pfnEnumSegments(pDbgMod, rtDbgModExportsAddSegmentsCallback, &Args);
155 if (RT_SUCCESS(rc))
156 {
157 rc = pDbgMod->pImgVt->pfnEnumSymbols(pDbgMod, RTLDR_ENUM_SYMBOL_FLAGS_ALL | RTLDR_ENUM_SYMBOL_FLAGS_NO_FWD,
158 Args.uImageBase ? Args.uImageBase : 0x10000,
159 rtDbgModExportsAddSymbolCallback, &Args);
160 if (RT_FAILURE(rc))
161 Log(("rtDbgModCreateForExports: Error during symbol enum: %Rrc\n", rc));
162 }
163 else
164 Log(("rtDbgModCreateForExports: Error during segment enum: %Rrc\n", rc));
165
166 /* This won't fail. */
167 if (RT_SUCCESS(rc))
168 rc = VINF_SUCCESS;
169 else
170 rc = -rc; /* Change it into a warning. */
171 return rc;
172}
173
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette