1 | /* $Id: ldr.cpp 37596 2011-06-22 19:30:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | /**
|
---|
44 | * Checks if a library is loadable or not.
|
---|
45 | *
|
---|
46 | * This may attempt load and unload the library.
|
---|
47 | *
|
---|
48 | * @returns true/false accordingly.
|
---|
49 | * @param pszFilename Image filename.
|
---|
50 | */
|
---|
51 | RTDECL(bool) RTLdrIsLoadable(const char *pszFilename)
|
---|
52 | {
|
---|
53 | /*
|
---|
54 | * Try to load the library.
|
---|
55 | */
|
---|
56 | RTLDRMOD hLib;
|
---|
57 | int rc = RTLdrLoad(pszFilename, &hLib);
|
---|
58 | if (RT_SUCCESS(rc))
|
---|
59 | {
|
---|
60 | RTLdrClose(hLib);
|
---|
61 | return true;
|
---|
62 | }
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 | RT_EXPORT_SYMBOL(RTLdrIsLoadable);
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Gets the address of a named exported symbol.
|
---|
70 | *
|
---|
71 | * @returns iprt status code.
|
---|
72 | * @param hLdrMod The loader module handle.
|
---|
73 | * @param pszSymbol Symbol name.
|
---|
74 | * @param ppvValue Where to store the symbol value. Note that this is restricted to the
|
---|
75 | * pointer size used on the host!
|
---|
76 | */
|
---|
77 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
|
---|
78 | {
|
---|
79 | LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
|
---|
80 | hLdrMod, pszSymbol, pszSymbol, ppvValue));
|
---|
81 | /*
|
---|
82 | * Validate input.
|
---|
83 | */
|
---|
84 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
85 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
86 | AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
|
---|
87 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
88 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Do it.
|
---|
92 | */
|
---|
93 | int rc;
|
---|
94 | if (pMod->pOps->pfnGetSymbol)
|
---|
95 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
|
---|
96 | else
|
---|
97 | {
|
---|
98 | RTUINTPTR Value = 0;
|
---|
99 | rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
|
---|
100 | if (RT_SUCCESS(rc))
|
---|
101 | {
|
---|
102 | *ppvValue = (void *)(uintptr_t)Value;
|
---|
103 | if ((uintptr_t)*ppvValue != Value)
|
---|
104 | rc = VERR_BUFFER_OVERFLOW;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 | RT_EXPORT_SYMBOL(RTLdrGetSymbol);
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Closes a loader module handle.
|
---|
115 | *
|
---|
116 | * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
|
---|
117 | * and RTLdrOpenBits() functions.
|
---|
118 | *
|
---|
119 | * @returns iprt status code.
|
---|
120 | * @param hLdrMod The loader module handle.
|
---|
121 | */
|
---|
122 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
|
---|
123 | {
|
---|
124 | LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Validate input.
|
---|
128 | */
|
---|
129 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
130 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
131 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Do it.
|
---|
135 | */
|
---|
136 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
137 | AssertRC(rc);
|
---|
138 | pMod->eState = LDR_STATE_INVALID;
|
---|
139 | pMod->u32Magic++;
|
---|
140 | RTMemFree(pMod);
|
---|
141 |
|
---|
142 | LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
|
---|
143 | return VINF_SUCCESS;
|
---|
144 | }
|
---|
145 | RT_EXPORT_SYMBOL(RTLdrClose);
|
---|
146 |
|
---|