1 | /* $Id: ldr.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Binary Image Loader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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 <iprt/alloc.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 | #include "internal/ldr.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Structures and Typedefs *
|
---|
43 | *******************************************************************************/
|
---|
44 | typedef struct RTLDRREADERFILE
|
---|
45 | {
|
---|
46 | /** The core. */
|
---|
47 | RTLDRREADER Core;
|
---|
48 | /** The file. */
|
---|
49 | RTFILE File;
|
---|
50 | /** The file size. */
|
---|
51 | RTFOFF cbFile;
|
---|
52 | /** The current offset. */
|
---|
53 | RTFOFF off;
|
---|
54 | /** The filename (variable size). */
|
---|
55 | char szFilename[1];
|
---|
56 | } RTLDRREADERFILE, *PRTLDRREADERFILE;
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Internal Functions *
|
---|
61 | *******************************************************************************/
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Gets the address of a named exported symbol.
|
---|
66 | *
|
---|
67 | * @returns iprt status code.
|
---|
68 | * @param hLdrMod The loader module handle.
|
---|
69 | * @param pszSymbol Symbol name.
|
---|
70 | * @param ppvValue Where to store the symbol value. Note that this is restricted to the
|
---|
71 | * pointer size used on the host!
|
---|
72 | */
|
---|
73 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
|
---|
74 | {
|
---|
75 | LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
|
---|
76 | hLdrMod, pszSymbol, pszSymbol, ppvValue));
|
---|
77 | /*
|
---|
78 | * Validate input.
|
---|
79 | */
|
---|
80 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
81 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
82 | AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
|
---|
83 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
84 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Do it.
|
---|
88 | */
|
---|
89 | int rc;
|
---|
90 | if (pMod->pOps->pfnGetSymbol)
|
---|
91 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
|
---|
92 | else
|
---|
93 | {
|
---|
94 | RTUINTPTR Value = 0;
|
---|
95 | rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | {
|
---|
98 | *ppvValue = (void *)Value;
|
---|
99 | if ((uintptr_t)*ppvValue != Value)
|
---|
100 | rc = VERR_BUFFER_OVERFLOW;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Closes a loader module handle.
|
---|
110 | *
|
---|
111 | * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
|
---|
112 | * and RTLdrOpenBits() functions.
|
---|
113 | *
|
---|
114 | * @returns iprt status code.
|
---|
115 | * @param hLdrMod The loader module handle.
|
---|
116 | */
|
---|
117 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
|
---|
118 | {
|
---|
119 | LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Validate input.
|
---|
123 | */
|
---|
124 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
125 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
126 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Do it.
|
---|
130 | */
|
---|
131 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
132 | AssertRC(rc);
|
---|
133 | pMod->eState = LDR_STATE_INVALID;
|
---|
134 | pMod->u32Magic++;
|
---|
135 | RTMemFree(pMod);
|
---|
136 |
|
---|
137 | LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
|
---|
138 | return VINF_SUCCESS;
|
---|
139 | }
|
---|
140 |
|
---|