| 1 |
""" Various rpython-level functions for dlopen |
| 2 |
""" |
| 3 |
|
| 4 |
from rpython.rtyper.tool import rffi_platform |
| 5 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 6 |
from rpython.rlib.objectmodel import we_are_translated, not_rpython |
| 7 |
from rpython.rlib.rarithmetic import r_uint |
| 8 |
from rpython.rlib.rutf8 import codepoints_in_utf8 |
| 9 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 10 |
from rpython.translator.platform import platform |
| 11 |
|
| 12 |
import sys, os, string |
| 13 |
|
| 14 |
|
| 15 |
_MSVC = platform.name == "msvc" |
| 16 |
_MINGW = platform.name == "mingw32" |
| 17 |
_WIN32 = _MSVC or _MINGW |
| 18 |
_MAC_OS = platform.name.startswith("darwin") |
| 19 |
_FREEBSD = sys.platform.startswith("freebsd") |
| 20 |
_NETBSD = sys.platform.startswith("netbsd") |
| 21 |
|
| 22 |
if _WIN32: |
| 23 |
from rpython.rlib import rwin32 |
| 24 |
includes = ['windows.h'] |
| 25 |
else: |
| 26 |
includes = ['dlfcn.h'] |
| 27 |
|
| 28 |
if _MAC_OS: |
| 29 |
pre_include_bits = ['#define MACOSX'] |
| 30 |
else: |
| 31 |
pre_include_bits = [] |
| 32 |
|
| 33 |
if _FREEBSD or _NETBSD or _WIN32: |
| 34 |
libraries = [] |
| 35 |
else: |
| 36 |
libraries = ['dl'] |
| 37 |
|
| 38 |
|
| 39 |
eci = ExternalCompilationInfo( |
| 40 |
pre_include_bits = pre_include_bits, |
| 41 |
includes = includes, |
| 42 |
libraries = libraries, |
| 43 |
) |
| 44 |
|
| 45 |
class CConfig: |
| 46 |
_compilation_info_ = eci |
| 47 |
|
| 48 |
RTLD_LOCAL = rffi_platform.DefinedConstantInteger('RTLD_LOCAL') |
| 49 |
RTLD_GLOBAL = rffi_platform.DefinedConstantInteger('RTLD_GLOBAL') |
| 50 |
RTLD_NOW = rffi_platform.DefinedConstantInteger('RTLD_NOW') |
| 51 |
RTLD_LAZY = rffi_platform.DefinedConstantInteger('RTLD_LAZY') |
| 52 |
RTLD_NODELETE = rffi_platform.DefinedConstantInteger('RTLD_NODELETE') |
| 53 |
RTLD_NOLOAD = rffi_platform.DefinedConstantInteger('RTLD_NOLOAD') |
| 54 |
RTLD_DEEPBIND = rffi_platform.DefinedConstantInteger('RTLD_DEEPBIND') |
| 55 |
|
| 56 |
class cConfig: |
| 57 |
pass |
| 58 |
|
| 59 |
for k, v in rffi_platform.configure(CConfig).items(): |
| 60 |
setattr(cConfig, k, v) |
| 61 |
|
| 62 |
def external(name, args, result, **kwds): |
| 63 |
return rffi.llexternal(name, args, result, compilation_info=eci, **kwds) |
| 64 |
|
| 65 |
class DLOpenError(Exception): |
| 66 |
def __init__(self, msg): |
| 67 |
self.msg = msg |
| 68 |
def __str__(self): |
| 69 |
return repr(self.msg) |
| 70 |
|
| 71 |
|
| 72 |
if not _WIN32: |
| 73 |
c_dlopen = external('dlopen', [rffi.CCHARP, rffi.INT], rffi.VOIDP) |
| 74 |
c_dlclose = external('dlclose', [rffi.VOIDP], rffi.INT, releasegil=False) |
| 75 |
c_dlerror = external('dlerror', [], rffi.CCHARP) |
| 76 |
c_dlsym = external('dlsym', [rffi.VOIDP, rffi.CCHARP], rffi.VOIDP) |
| 77 |
|
| 78 |
DLLHANDLE = rffi.VOIDP |
| 79 |
|
| 80 |
RTLD_LOCAL = cConfig.RTLD_LOCAL |
| 81 |
RTLD_GLOBAL = cConfig.RTLD_GLOBAL |
| 82 |
RTLD_NOW = cConfig.RTLD_NOW |
| 83 |
RTLD_LAZY = cConfig.RTLD_LAZY |
| 84 |
|
| 85 |
def dlerror(): |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if not we_are_translated(): |
| 90 |
return "error info not available, not translated" |
| 91 |
res = c_dlerror() |
| 92 |
if not res: |
| 93 |
return "" |
| 94 |
return rffi.charp2str(res) |
| 95 |
|
| 96 |
@not_rpython |
| 97 |
def _dlerror_on_dlopen_untranslated(name): |
| 98 |
|
| 99 |
import ctypes |
| 100 |
try: |
| 101 |
ctypes.CDLL(name) |
| 102 |
except OSError as e: |
| 103 |
|
| 104 |
|
| 105 |
return str(e) |
| 106 |
else: |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if os.path.exists(name) and "__pypy__" in sys.builtin_module_names: |
| 115 |
return ( |
| 116 |
"%s: opening with ctypes.CDLL() works but not with " |
| 117 |
"c_dlopen()! On PyPy, so possible invalid ELF header?" |
| 118 |
% (name,) |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
return ("opening %r with ctypes.CDLL() works, " |
| 123 |
"but not with c_dlopen()??" % (name,)) |
| 124 |
|
| 125 |
def _retry_as_ldscript(err, mode): |
| 126 |
""" ld scripts are fairly straightforward to parse (the library we want |
| 127 |
is in a form like 'GROUP ( <actual-filepath.so>'. A simple state machine |
| 128 |
can parse that out (avoids regexes).""" |
| 129 |
|
| 130 |
parts = err.split(":") |
| 131 |
if len(parts) != 2: |
| 132 |
return lltype.nullptr(rffi.VOIDP.TO) |
| 133 |
fullpath = parts[0] |
| 134 |
actual = "" |
| 135 |
last_five = " " |
| 136 |
state = 0 |
| 137 |
ldscript = os.open(fullpath, os.O_RDONLY, 0777) |
| 138 |
c = os.read(ldscript, 1) |
| 139 |
while c != "": |
| 140 |
if state == 0: |
| 141 |
last_five += c |
| 142 |
last_five = last_five[1:6] |
| 143 |
if last_five == "GROUP": |
| 144 |
state = 1 |
| 145 |
elif state == 1: |
| 146 |
if c == "(": |
| 147 |
state = 2 |
| 148 |
elif state == 2: |
| 149 |
if c not in string.whitespace: |
| 150 |
actual += c |
| 151 |
state = 3 |
| 152 |
elif state == 3: |
| 153 |
if c in string.whitespace or c == ")": |
| 154 |
break |
| 155 |
else: |
| 156 |
actual += c |
| 157 |
c = os.read(ldscript, 1) |
| 158 |
os.close(ldscript) |
| 159 |
if actual != "": |
| 160 |
a = rffi.str2charp(actual) |
| 161 |
lib = c_dlopen(a, rffi.cast(rffi.INT, mode)) |
| 162 |
rffi.free_charp(a) |
| 163 |
return lib |
| 164 |
else: |
| 165 |
return lltype.nullptr(rffi.VOIDP.TO) |
| 166 |
|
| 167 |
def _dlopen_default_mode(): |
| 168 |
""" The default dlopen mode if it hasn't been changed by the user. |
| 169 |
""" |
| 170 |
mode = RTLD_NOW |
| 171 |
if RTLD_LOCAL is not None: |
| 172 |
mode |= RTLD_LOCAL |
| 173 |
return mode |
| 174 |
|
| 175 |
def dlopen(name, mode=-1): |
| 176 |
""" Wrapper around C-level dlopen |
| 177 |
""" |
| 178 |
if mode == -1: |
| 179 |
mode = _dlopen_default_mode() |
| 180 |
elif (mode & (RTLD_LAZY | RTLD_NOW)) == 0: |
| 181 |
mode |= RTLD_NOW |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if not we_are_translated() and platform.name == "linux": |
| 186 |
if name == 'libm.so': |
| 187 |
name = 'libm.so.6' |
| 188 |
|
| 189 |
with rffi.scoped_str2charp(name) as ll_libname: |
| 190 |
res = c_dlopen(ll_libname, rffi.cast(rffi.INT, mode)) |
| 191 |
if not res: |
| 192 |
if not we_are_translated(): |
| 193 |
err = _dlerror_on_dlopen_untranslated(name) |
| 194 |
else: |
| 195 |
err = dlerror() |
| 196 |
if platform.name == "linux" and 'invalid ELF header' in err: |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
res = _retry_as_ldscript(err, mode) |
| 202 |
if not res: |
| 203 |
raise DLOpenError(err) |
| 204 |
return res |
| 205 |
else: |
| 206 |
raise DLOpenError(err) |
| 207 |
return res |
| 208 |
|
| 209 |
dlclose = c_dlclose |
| 210 |
|
| 211 |
def dlsym(libhandle, name): |
| 212 |
""" Wrapper around C-level dlsym |
| 213 |
""" |
| 214 |
res = c_dlsym(libhandle, name) |
| 215 |
if not res: |
| 216 |
raise KeyError(name) |
| 217 |
|
| 218 |
return res |
| 219 |
|
| 220 |
def dlsym_byordinal(handle, index): |
| 221 |
|
| 222 |
raise KeyError(index) |
| 223 |
|
| 224 |
else: |
| 225 |
DLLHANDLE = rwin32.HMODULE |
| 226 |
RTLD_GLOBAL = None |
| 227 |
|
| 228 |
def _dlopen_default_mode(): |
| 229 |
""" The default dlopen mode if it hasn't been changed by the user. |
| 230 |
""" |
| 231 |
return 0 |
| 232 |
|
| 233 |
def dlopen(name, mode=-1): |
| 234 |
|
| 235 |
if not name: |
| 236 |
raise DLOpenError("cannot use None") |
| 237 |
with rffi.scoped_utf82wcharp(name, codepoints_in_utf8(name)) as buf: |
| 238 |
res = rwin32.LoadLibraryW(buf) |
| 239 |
if not res: |
| 240 |
err = rwin32.GetLastError_saved() |
| 241 |
ustr, lgt = rwin32.FormatErrorW(err) |
| 242 |
raise DLOpenError(ustr) |
| 243 |
return res |
| 244 |
|
| 245 |
def dlopenex(name, flags=rwin32.LOAD_WITH_ALTERED_SEARCH_PATH): |
| 246 |
|
| 247 |
if not name: |
| 248 |
raise DLOpenError("cannot use None") |
| 249 |
old_mode = rwin32.SetErrorMode(rwin32.SEM_FAILCRITICALERRORS) |
| 250 |
with rffi.scoped_utf82wcharp(name, codepoints_in_utf8(name)) as buf: |
| 251 |
res = rwin32.LoadLibraryExW(buf, flags) |
| 252 |
rwin32.SetErrorMode(old_mode) |
| 253 |
if not res: |
| 254 |
err = rwin32.GetLastError_saved() |
| 255 |
ustr, lgt = rwin32.FormatErrorW(err) |
| 256 |
raise DLOpenError(ustr) |
| 257 |
return res |
| 258 |
|
| 259 |
def dlclose(handle): |
| 260 |
res = rwin32.FreeLibrary(handle) |
| 261 |
if res: |
| 262 |
return 0 |
| 263 |
else: |
| 264 |
return -1 |
| 265 |
|
| 266 |
def dlsym(handle, name): |
| 267 |
res = rwin32.GetProcAddress(handle, name) |
| 268 |
if not res: |
| 269 |
raise KeyError(name) |
| 270 |
|
| 271 |
return res |
| 272 |
|
| 273 |
def dlsym_byordinal(handle, index): |
| 274 |
|
| 275 |
intresource = rffi.cast(rffi.CCHARP, r_uint(index) & 0xFFFF) |
| 276 |
res = rwin32.GetProcAddress(handle, intresource) |
| 277 |
if not res: |
| 278 |
raise KeyError(index) |
| 279 |
|
| 280 |
return res |
| 281 |
|
| 282 |
GetModuleHandle = rwin32.GetModuleHandle |