VirtualBox

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

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

RTLdrIsLoadable: docs, leave validation to RTLdrLoad.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1/* $Id: ldr.cpp 17019 2009-02-23 13:34:39Z 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.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 * 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/**
65 * Checks if a library is loadable or not.
66 *
67 * This may attempt load and unload the library.
68 *
69 * @returns true/false accordingly.
70 * @param pszFilename Image filename.
71 */
72RTDECL(bool) RTLdrIsLoadable(const char *pszFilename)
73{
74 /*
75 * Try to load the library.
76 */
77 RTLDRMOD hLib;
78 int rc = RTLdrLoad(pszFilename, &hLib);
79 if (RT_SUCCESS(rc))
80 {
81 RTLdrClose(hLib);
82 return true;
83 }
84 return false;
85}
86
87
88/**
89 * Gets the address of a named exported symbol.
90 *
91 * @returns iprt status code.
92 * @param hLdrMod The loader module handle.
93 * @param pszSymbol Symbol name.
94 * @param ppvValue Where to store the symbol value. Note that this is restricted to the
95 * pointer size used on the host!
96 */
97RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
98{
99 LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
100 hLdrMod, pszSymbol, pszSymbol, ppvValue));
101 /*
102 * Validate input.
103 */
104 AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
105 AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
106 AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
107 PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
108 //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
109
110 /*
111 * Do it.
112 */
113 int rc;
114 if (pMod->pOps->pfnGetSymbol)
115 rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
116 else
117 {
118 RTUINTPTR Value = 0;
119 rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
120 if (RT_SUCCESS(rc))
121 {
122 *ppvValue = (void *)Value;
123 if ((uintptr_t)*ppvValue != Value)
124 rc = VERR_BUFFER_OVERFLOW;
125 }
126 }
127 LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
128 return rc;
129}
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 */
141RTDECL(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
Note: See TracBrowser for help on using the repository browser.

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