1 | /* $Id: ldr.cpp 49044 2013-10-11 01:06:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2013 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(bool) RTLdrIsLoadable(const char *pszFilename)
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Try to load the library.
|
---|
47 | */
|
---|
48 | RTLDRMOD hLib;
|
---|
49 | int rc = RTLdrLoad(pszFilename, &hLib);
|
---|
50 | if (RT_SUCCESS(rc))
|
---|
51 | {
|
---|
52 | RTLdrClose(hLib);
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | RT_EXPORT_SYMBOL(RTLdrIsLoadable);
|
---|
58 |
|
---|
59 |
|
---|
60 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
|
---|
61 | {
|
---|
62 | LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
|
---|
63 | hLdrMod, pszSymbol, pszSymbol, ppvValue));
|
---|
64 | /*
|
---|
65 | * Validate input.
|
---|
66 | */
|
---|
67 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
68 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
69 | AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
|
---|
70 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
71 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Do it.
|
---|
75 | */
|
---|
76 | int rc;
|
---|
77 | if (pMod->pOps->pfnGetSymbol)
|
---|
78 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
|
---|
79 | else
|
---|
80 | {
|
---|
81 | RTUINTPTR Value = 0;
|
---|
82 | rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | {
|
---|
85 | *ppvValue = (void *)(uintptr_t)Value;
|
---|
86 | if ((uintptr_t)*ppvValue != Value)
|
---|
87 | rc = VERR_BUFFER_OVERFLOW;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 | RT_EXPORT_SYMBOL(RTLdrGetSymbol);
|
---|
94 |
|
---|
95 |
|
---|
96 | RTDECL(PFNRT) RTLdrGetFunction(RTLDRMOD hLdrMod, const char *pszSymbol)
|
---|
97 | {
|
---|
98 | PFNRT pfn;
|
---|
99 | int rc = RTLdrGetSymbol(hLdrMod, pszSymbol, (void **)&pfn);
|
---|
100 | if (RT_SUCCESS(rc))
|
---|
101 | return pfn;
|
---|
102 | return NULL;
|
---|
103 | }
|
---|
104 | RT_EXPORT_SYMBOL(RTLdrGetFunction);
|
---|
105 |
|
---|
106 |
|
---|
107 | RTDECL(RTLDRFMT) RTLdrGetFormat(RTLDRMOD hLdrMod)
|
---|
108 | {
|
---|
109 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRFMT_INVALID);
|
---|
110 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
111 | return pMod->enmFormat;
|
---|
112 | }
|
---|
113 | RT_EXPORT_SYMBOL(RTLdrGetFormat);
|
---|
114 |
|
---|
115 |
|
---|
116 | RTDECL(RTLDRTYPE) RTLdrGetType(RTLDRMOD hLdrMod)
|
---|
117 | {
|
---|
118 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRTYPE_INVALID);
|
---|
119 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
120 | return pMod->enmType;
|
---|
121 | }
|
---|
122 | RT_EXPORT_SYMBOL(RTLdrGetType);
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(RTLDRENDIAN) RTLdrGetEndian(RTLDRMOD hLdrMod)
|
---|
126 | {
|
---|
127 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRENDIAN_INVALID);
|
---|
128 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
129 | return pMod->enmEndian;
|
---|
130 | }
|
---|
131 | RT_EXPORT_SYMBOL(RTLdrGetEndian);
|
---|
132 |
|
---|
133 |
|
---|
134 | RTDECL(RTLDRARCH) RTLdrGetArch(RTLDRMOD hLdrMod)
|
---|
135 | {
|
---|
136 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), RTLDRARCH_INVALID);
|
---|
137 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
138 | return pMod->enmArch;
|
---|
139 | }
|
---|
140 | RT_EXPORT_SYMBOL(RTLdrGetArch);
|
---|
141 |
|
---|
142 |
|
---|
143 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
|
---|
144 | {
|
---|
145 | LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Validate input.
|
---|
149 | */
|
---|
150 | if (hLdrMod == NIL_RTLDRMOD)
|
---|
151 | return VINF_SUCCESS;
|
---|
152 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
153 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
154 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * Do it.
|
---|
158 | */
|
---|
159 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
160 | AssertRC(rc);
|
---|
161 | pMod->eState = LDR_STATE_INVALID;
|
---|
162 | pMod->u32Magic++;
|
---|
163 | if (pMod->pReader)
|
---|
164 | {
|
---|
165 | rc = pMod->pReader->pfnDestroy(pMod->pReader);
|
---|
166 | AssertRC(rc);
|
---|
167 | pMod->pReader = NULL;
|
---|
168 | }
|
---|
169 | RTMemFree(pMod);
|
---|
170 |
|
---|
171 | LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
|
---|
172 | return VINF_SUCCESS;
|
---|
173 | }
|
---|
174 | RT_EXPORT_SYMBOL(RTLdrClose);
|
---|
175 |
|
---|