VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldr.cpp@ 17010

Last change on this file since 17010 was 17010, checked in by vboxsync, 16 years ago

iprt: RTLdrIsLoadable for checking if an dynamically library could be loaded.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/* $Id: ldr.cpp 17010 2009-02-23 12:25:06Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_LDR
36#include <iprt/ldr.h>
37#include <iprt/alloc.h>
38#include <iprt/string.h>
39#include <iprt/assert.h>
40#include <iprt/err.h>
41#include <iprt/log.h>
42#include "internal/ldr.h"
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48typedef struct RTLDRREADERFILE
49{
50 /** The core. */
51 RTLDRREADER Core;
52 /** The file. */
53 RTFILE File;
54 /** The file size. */
55 RTFOFF cbFile;
56 /** The current offset. */
57 RTFOFF off;
58 /** The filename (variable size). */
59 char szFilename[1];
60} RTLDRREADERFILE, *PRTLDRREADERFILE;
61
62
63/*******************************************************************************
64* Internal Functions *
65*******************************************************************************/
66
67RTDECL(bool) RTLdrIsLoadable(const char *pszName)
68{
69 /*
70 * Quick validation.
71 */
72 if (!pszName)
73 return false;
74
75 bool fLoadable = false;
76 RTLDRMOD hLib;
77 /*
78 * Try to load the library.
79 */
80 int rc = RTLdrLoad(pszName, &hLib);
81 if (RT_SUCCESS(rc))
82 {
83 fLoadable = true;
84 RTLdrClose(hLib);
85 }
86 return fLoadable;
87}
88
89/**
90 * Gets the address of a named exported symbol.
91 *
92 * @returns iprt status code.
93 * @param hLdrMod The loader module handle.
94 * @param pszSymbol Symbol name.
95 * @param ppvValue Where to store the symbol value. Note that this is restricted to the
96 * pointer size used on the host!
97 */
98RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
99{
100 LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
101 hLdrMod, pszSymbol, pszSymbol, ppvValue));
102 /*
103 * Validate input.
104 */
105 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
106 AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
107 AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
108 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
109 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
110
111 /*
112 * Do it.
113 */
114 int rc;
115 if (pMod->pOps->pfnGetSymbol)
116 rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
117 else
118 {
119 RTUINTPTR Value = 0;
120 rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
121 if (RT_SUCCESS(rc))
122 {
123 *ppvValue = (void *)Value;
124 if ((uintptr_t)*ppvValue != Value)
125 rc = VERR_BUFFER_OVERFLOW;
126 }
127 }
128 LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
129 return rc;
130}
131
132
133/**
134 * Closes a loader module handle.
135 *
136 * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
137 * and RTLdrOpenBits() functions.
138 *
139 * @returns iprt status code.
140 * @param hLdrMod The loader module handle.
141 */
142RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
143{
144 LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
145
146 /*
147 * Validate input.
148 */
149 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
150 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
151 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
152
153 /*
154 * Do it.
155 */
156 int rc = pMod->pOps->pfnClose(pMod);
157 AssertRC(rc);
158 pMod->eState = LDR_STATE_INVALID;
159 pMod->u32Magic++;
160 RTMemFree(pMod);
161
162 LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
163 return VINF_SUCCESS;
164}
165
Note: See TracBrowser for help on using the repository browser.

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