1 | /* $Id: ldr.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 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_LDR
|
---|
32 | #include <iprt/ldr.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/log.h>
|
---|
40 | #include "internal/ldr.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
|
---|
44 | {
|
---|
45 | LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
|
---|
46 | hLdrMod, pszSymbol, pszSymbol, ppvValue));
|
---|
47 | /*
|
---|
48 | * Validate input.
|
---|
49 | */
|
---|
50 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
51 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
52 | AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
|
---|
53 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
54 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Do it.
|
---|
58 | */
|
---|
59 | int rc;
|
---|
60 | if (pMod->pOps->pfnGetSymbol)
|
---|
61 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
|
---|
62 | else
|
---|
63 | {
|
---|
64 | RTUINTPTR Value = 0;
|
---|
65 | rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, UINT32_MAX, pszSymbol, &Value);
|
---|
66 | if (RT_SUCCESS(rc))
|
---|
67 | {
|
---|
68 | *ppvValue = (void *)(uintptr_t)Value;
|
---|
69 | if ((uintptr_t)*ppvValue != Value)
|
---|
70 | rc = VERR_BUFFER_OVERFLOW;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
|
---|
74 | return rc;
|
---|
75 | }
|
---|
76 | RT_EXPORT_SYMBOL(RTLdrGetSymbol);
|
---|
77 |
|
---|
78 |
|
---|
79 | RTDECL(PFNRT) RTLdrGetFunction(RTLDRMOD hLdrMod, const char *pszSymbol)
|
---|
80 | {
|
---|
81 | PFNRT pfn;
|
---|
82 | int rc = RTLdrGetSymbol(hLdrMod, pszSymbol, (void **)&pfn);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | return pfn;
|
---|
85 | return NULL;
|
---|
86 | }
|
---|
87 | RT_EXPORT_SYMBOL(RTLdrGetFunction);
|
---|
88 |
|
---|
89 |
|
---|
90 | RTDECL(RTLDRFMT) RTLdrGetFormat(RTLDRMOD hLdrMod)
|
---|
91 | {
|
---|
92 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRFMT_INVALID);
|
---|
93 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
94 | return pMod->enmFormat;
|
---|
95 | }
|
---|
96 | RT_EXPORT_SYMBOL(RTLdrGetFormat);
|
---|
97 |
|
---|
98 |
|
---|
99 | RTDECL(RTLDRTYPE) RTLdrGetType(RTLDRMOD hLdrMod)
|
---|
100 | {
|
---|
101 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRTYPE_INVALID);
|
---|
102 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
103 | return pMod->enmType;
|
---|
104 | }
|
---|
105 | RT_EXPORT_SYMBOL(RTLdrGetType);
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(RTLDRENDIAN) RTLdrGetEndian(RTLDRMOD hLdrMod)
|
---|
109 | {
|
---|
110 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRENDIAN_INVALID);
|
---|
111 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
112 | return pMod->enmEndian;
|
---|
113 | }
|
---|
114 | RT_EXPORT_SYMBOL(RTLdrGetEndian);
|
---|
115 |
|
---|
116 |
|
---|
117 | RTDECL(RTLDRARCH) RTLdrGetArch(RTLDRMOD hLdrMod)
|
---|
118 | {
|
---|
119 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRARCH_INVALID);
|
---|
120 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
121 | return pMod->enmArch;
|
---|
122 | }
|
---|
123 | RT_EXPORT_SYMBOL(RTLdrGetArch);
|
---|
124 |
|
---|
125 |
|
---|
126 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
|
---|
127 | {
|
---|
128 | LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Validate input.
|
---|
132 | */
|
---|
133 | if (hLdrMod == NIL_RTLDRMOD)
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
136 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
137 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Do it.
|
---|
141 | */
|
---|
142 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
143 | AssertRC(rc);
|
---|
144 | pMod->eState = LDR_STATE_INVALID;
|
---|
145 | pMod->u32Magic++;
|
---|
146 | if (pMod->pReader)
|
---|
147 | {
|
---|
148 | rc = pMod->pReader->pfnDestroy(pMod->pReader);
|
---|
149 | AssertRC(rc);
|
---|
150 | pMod->pReader = NULL;
|
---|
151 | }
|
---|
152 | RTMemFree(pMod);
|
---|
153 |
|
---|
154 | LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
|
---|
155 | return VINF_SUCCESS;
|
---|
156 | }
|
---|
157 | RT_EXPORT_SYMBOL(RTLdrClose);
|
---|
158 |
|
---|