VirtualBox

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

Last change on this file since 21848 was 21337, checked in by vboxsync, 15 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

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