1 | /* $Id: ldr.cpp 2981 2007-06-01 16:01:28Z 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.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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
27 | #include <iprt/ldr.h>
|
---|
28 | #include <iprt/alloc.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/err.h>
|
---|
32 | #include <iprt/log.h>
|
---|
33 | #include "internal/ldr.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Structures and Typedefs *
|
---|
38 | *******************************************************************************/
|
---|
39 | typedef struct RTLDRREADERFILE
|
---|
40 | {
|
---|
41 | /** The core. */
|
---|
42 | RTLDRREADER Core;
|
---|
43 | /** The file. */
|
---|
44 | RTFILE File;
|
---|
45 | /** The file size. */
|
---|
46 | RTFOFF cbFile;
|
---|
47 | /** The current offset. */
|
---|
48 | RTFOFF off;
|
---|
49 | /** The filename (variable size). */
|
---|
50 | char szFilename[1];
|
---|
51 | } RTLDRREADERFILE, *PRTLDRREADERFILE;
|
---|
52 |
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Internal Functions *
|
---|
56 | *******************************************************************************/
|
---|
57 |
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Gets the address of a named exported symbol.
|
---|
61 | *
|
---|
62 | * @returns iprt status code.
|
---|
63 | * @param hLdrMod The loader module handle.
|
---|
64 | * @param pszSymbol Symbol name.
|
---|
65 | * @param ppvValue Where to store the symbol value. Note that this is restricted to the
|
---|
66 | * pointer size used on the host!
|
---|
67 | */
|
---|
68 | RTDECL(int) RTLdrGetSymbol(RTLDRMOD hLdrMod, const char *pszSymbol, void **ppvValue)
|
---|
69 | {
|
---|
70 | LogFlow(("RTLdrGetSymbol: hLdrMod=%RTldrm pszSymbol=%p:{%s} ppvValue=%p\n",
|
---|
71 | hLdrMod, pszSymbol, pszSymbol, ppvValue));
|
---|
72 | /*
|
---|
73 | * Validate input.
|
---|
74 | */
|
---|
75 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
76 | AssertMsgReturn(pszSymbol, ("pszSymbol=%p\n", pszSymbol), VERR_INVALID_PARAMETER);
|
---|
77 | AssertMsgReturn(VALID_PTR(ppvValue), ("ppvValue=%p\n", ppvValue), VERR_INVALID_PARAMETER);
|
---|
78 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
79 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Do it.
|
---|
83 | */
|
---|
84 | int rc;
|
---|
85 | if (pMod->pOps->pfnGetSymbol)
|
---|
86 | rc = pMod->pOps->pfnGetSymbol(pMod, pszSymbol, ppvValue);
|
---|
87 | else
|
---|
88 | {
|
---|
89 | RTUINTPTR Value = 0;
|
---|
90 | rc = pMod->pOps->pfnGetSymbolEx(pMod, NULL, 0, pszSymbol, &Value);
|
---|
91 | if (RT_SUCCESS(rc))
|
---|
92 | {
|
---|
93 | *ppvValue = (void *)Value;
|
---|
94 | if ((uintptr_t)*ppvValue != Value)
|
---|
95 | rc = VERR_BUFFER_OVERFLOW;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | LogFlow(("RTLdrGetSymbol: return %Rrc *ppvValue=%p\n", rc, *ppvValue));
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Closes a loader module handle.
|
---|
105 | *
|
---|
106 | * The handle can be obtained using any of the RTLdrLoad(), RTLdrOpen()
|
---|
107 | * and RTLdrOpenBits() functions.
|
---|
108 | *
|
---|
109 | * @returns iprt status code.
|
---|
110 | * @param hLdrMod The loader module handle.
|
---|
111 | */
|
---|
112 | RTDECL(int) RTLdrClose(RTLDRMOD hLdrMod)
|
---|
113 | {
|
---|
114 | LogFlow(("RTLdrClose: hLdrMod=%RTldrm\n", hLdrMod));
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Validate input.
|
---|
118 | */
|
---|
119 | AssertMsgReturn(rtldrIsValid(hLdrMod), ("hLdrMod=%p\n", hLdrMod), VERR_INVALID_HANDLE);
|
---|
120 | PRTLDRMODINTERNAL pMod = (PRTLDRMODINTERNAL)hLdrMod;
|
---|
121 | //AssertMsgReturn(pMod->eState == LDR_STATE_OPENED, ("eState=%d\n", pMod->eState), VERR_WRONG_ORDER);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Do it.
|
---|
125 | */
|
---|
126 | int rc = pMod->pOps->pfnClose(pMod);
|
---|
127 | AssertRC(rc);
|
---|
128 | pMod->eState = LDR_STATE_INVALID;
|
---|
129 | pMod->u32Magic++;
|
---|
130 | RTMemFree(pMod);
|
---|
131 |
|
---|
132 | LogFlow(("RTLdrClose: returns VINF_SUCCESS\n"));
|
---|
133 | return VINF_SUCCESS;
|
---|
134 | }
|
---|
135 |
|
---|