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