1 | /* $Id: ldrNative-posix.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Binary Image Loader, POSIX native.
|
---|
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 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
26 | #include <dlfcn.h>
|
---|
27 |
|
---|
28 | #include <iprt/ldr.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/path.h>
|
---|
31 | #include <iprt/alloca.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/log.h>
|
---|
35 | #include "internal/ldr.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | int rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle)
|
---|
39 | {
|
---|
40 | /*
|
---|
41 | * Do we need to add an extension?
|
---|
42 | */
|
---|
43 | if (!RTPathHaveExt(pszFilename))
|
---|
44 | {
|
---|
45 | #if defined(__OS2__) || defined(__WIN__)
|
---|
46 | static const char s_szSuff[] = ".DLL";
|
---|
47 | #elif defined(__L4__)
|
---|
48 | static const char s_szSuff[] = ".s.so";
|
---|
49 | #elif defined(__DARWIN__)
|
---|
50 | static const char s_szSuff[] = ".dylib";
|
---|
51 | #else
|
---|
52 | static const char s_szSuff[] = ".so";
|
---|
53 | #endif
|
---|
54 | size_t cch = strlen(pszFilename);
|
---|
55 | char *psz = (char *)alloca(cch + sizeof(s_szSuff));
|
---|
56 | if (!psz)
|
---|
57 | return VERR_NO_MEMORY;
|
---|
58 | memcpy(psz, pszFilename, cch);
|
---|
59 | memcpy(psz + cch, s_szSuff, sizeof(s_szSuff));
|
---|
60 | pszFilename = psz;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Attempt load.
|
---|
65 | */
|
---|
66 |
|
---|
67 | void *pvMod = dlopen(pszFilename, RTLD_NOW | RTLD_LOCAL);
|
---|
68 | if (pvMod)
|
---|
69 | {
|
---|
70 | *phHandle = (uintptr_t)pvMod;
|
---|
71 | return VINF_SUCCESS;
|
---|
72 | }
|
---|
73 | Log(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, dlerror()));
|
---|
74 | return VERR_FILE_NOT_FOUND;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
|
---|
79 | {
|
---|
80 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
81 | #ifdef __OS2__
|
---|
82 | /* Prefix the symbol with an underscore (assuming __cdecl/gcc-default). */
|
---|
83 | size_t cch = strlen(pszSymbol);
|
---|
84 | char *psz = (char *)alloca(cch + 2);
|
---|
85 | psz[0] = '_';
|
---|
86 | memcpy(psz + 1, pszSymbol, cch + 1);
|
---|
87 | pszSymbol = psz;
|
---|
88 | #endif
|
---|
89 | *ppvValue = dlsym((void *)pModNative->hNative, pszSymbol);
|
---|
90 | if (*ppvValue)
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | return VERR_SYMBOL_NOT_FOUND;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
|
---|
97 | {
|
---|
98 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
99 | if (!dlclose((void *)pModNative->hNative))
|
---|
100 | {
|
---|
101 | pModNative->hNative = (uintptr_t)0;
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 | Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
|
---|
105 | return VERR_GENERAL_FAILURE;
|
---|
106 | }
|
---|
107 |
|
---|