1 | /* $Id: ldr.cpp 28800 2010-04-27 08:22:32Z 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 | * Structures and Typedefs *
|
---|
45 | *******************************************************************************/
|
---|
46 | typedef struct RTLDRREADERFILE
|
---|
47 | {
|
---|
48 | /** The core. */
|
---|
49 | RTLDRREADER Core;
|
---|
50 | /** The file. */
|
---|
51 | RTFILE File;
|
---|
52 | /** The file size. */
|
---|
53 | RTFOFF cbFile;
|
---|
54 | /** The current offset. */
|
---|
55 | RTFOFF off;
|
---|
56 | /** The filename (variable size). */
|
---|
57 | char szFilename[1];
|
---|
58 | } RTLDRREADERFILE, *PRTLDRREADERFILE;
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Checks if a library is loadable or not.
|
---|
64 | *
|
---|
65 | * This may attempt load and unload the library.
|
---|
66 | *
|
---|
67 | * @returns true/false accordingly.
|
---|
68 | * @param pszFilename Image filename.
|
---|
69 | */
|
---|
70 | RTDECL(bool) RTLdrIsLoadable(const char *pszFilename)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Try to load the library.
|
---|
74 | */
|
---|
75 | RTLDRMOD hLib;
|
---|
76 | int rc = RTLdrLoad(pszFilename, &hLib);
|
---|
77 | if (RT_SUCCESS(rc))
|
---|
78 | {
|
---|
79 | RTLdrClose(hLib);
|
---|
80 | return true;
|
---|
81 | }
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 | RT_EXPORT_SYMBOL(RTLdrIsLoadable);
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Gets the address of a named exported symbol.
|
---|
89 | *
|
---|
90 | * @returns iprt status code.
|
---|
91 | * @param hLdrMod The loader module handle.
|
---|
92 | * @param pszSymbol Symbol name.
|
---|
93 | * @param ppvValue Where to store the symbol value. Note that this is restricted to the
|
---|
94 | * pointer size used on the host!
|
---|
95 | */
|
---|
96 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
|
---|
97 | {
|
---|
98 | LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
|
---|
99 | hLdrMod, pszSymbol, pszSymbol, ppvValue));
|
---|
100 | /*
|
---|
101 | * Validate input.
|
---|
102 | */
|
---|
103 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
104 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
105 | AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
|
---|
106 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
107 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Do it.
|
---|
111 | */
|
---|
112 | int rc;
|
---|
113 | if (pMod->pOps->pfnGetSymbol)
|
---|
114 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
|
---|
115 | else
|
---|
116 | {
|
---|
117 | RTUINTPTR Value = 0;
|
---|
118 | rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
|
---|
119 | if (RT_SUCCESS(rc))
|
---|
120 | {
|
---|
121 | *ppvValue = (void *)(uintptr_t)Value;
|
---|
122 | if ((uintptr_t)*ppvValue != Value)
|
---|
123 | rc = VERR_BUFFER_OVERFLOW;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTLdrGetSymbol);
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Closes a loader module handle.
|
---|
134 | *
|
---|
135 | * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
|
---|
136 | * and RTLdrOpenBits() functions.
|
---|
137 | *
|
---|
138 | * @returns iprt status code.
|
---|
139 | * @param hLdrMod The loader module handle.
|
---|
140 | */
|
---|
141 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
|
---|
142 | {
|
---|
143 | LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Validate input.
|
---|
147 | */
|
---|
148 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
149 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
150 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Do it.
|
---|
154 | */
|
---|
155 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
156 | AssertRC(rc);
|
---|
157 | pMod->eState = LDR_STATE_INVALID;
|
---|
158 | pMod->u32Magic++;
|
---|
159 | RTMemFree(pMod);
|
---|
160 |
|
---|
161 | LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
|
---|
162 | return VINF_SUCCESS;
|
---|
163 | }
|
---|
164 | RT_EXPORT_SYMBOL(RTLdrClose);
|
---|
165 |
|
---|