| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'fiddle.so' |
| 4 |
require 'fiddle/closure' |
| 5 |
require 'fiddle/function' |
| 6 |
require 'fiddle/version' |
| 7 |
|
| 8 |
module Fiddle |
| 9 |
if WINDOWS |
| 10 |
# Returns the last win32 +Error+ of the current executing +Thread+ or nil |
| 11 |
# if none |
| 12 |
def self.win32_last_error |
| 13 |
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] |
| 14 |
end |
| 15 |
|
| 16 |
# Sets the last win32 +Error+ of the current executing +Thread+ to +error+ |
| 17 |
def self.win32_last_error= error |
| 18 |
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error |
| 19 |
end |
| 20 |
|
| 21 |
# Returns the last win32 socket +Error+ of the current executing |
| 22 |
# +Thread+ or nil if none |
| 23 |
def self.win32_last_socket_error |
| 24 |
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] |
| 25 |
end |
| 26 |
|
| 27 |
# Sets the last win32 socket +Error+ of the current executing |
| 28 |
# +Thread+ to +error+ |
| 29 |
def self.win32_last_socket_error= error |
| 30 |
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] = error |
| 31 |
end |
| 32 |
end |
| 33 |
|
| 34 |
# Returns the last +Error+ of the current executing +Thread+ or nil if none |
| 35 |
def self.last_error |
| 36 |
Thread.current[:__FIDDLE_LAST_ERROR__] |
| 37 |
end |
| 38 |
|
| 39 |
# Sets the last +Error+ of the current executing +Thread+ to +error+ |
| 40 |
def self.last_error= error |
| 41 |
Thread.current[:__DL2_LAST_ERROR__] = error |
| 42 |
Thread.current[:__FIDDLE_LAST_ERROR__] = error |
| 43 |
end |
| 44 |
|
| 45 |
# call-seq: dlopen(library) => Fiddle::Handle |
| 46 |
# |
| 47 |
# Creates a new handler that opens +library+, and returns an instance of |
| 48 |
# Fiddle::Handle. |
| 49 |
# |
| 50 |
# If +nil+ is given for the +library+, Fiddle::Handle::DEFAULT is used, which |
| 51 |
# is the equivalent to RTLD_DEFAULT. See <code>man 3 dlopen</code> for more. |
| 52 |
# |
| 53 |
# lib = Fiddle.dlopen(nil) |
| 54 |
# |
| 55 |
# The default is dependent on OS, and provide a handle for all libraries |
| 56 |
# already loaded. For example, in most cases you can use this to access |
| 57 |
# +libc+ functions, or ruby functions like +rb_str_new+. |
| 58 |
# |
| 59 |
# See Fiddle::Handle.new for more. |
| 60 |
def dlopen library |
| 61 |
begin |
| 62 |
Fiddle::Handle.new(library) |
| 63 |
rescue DLError => error |
| 64 |
case RUBY_PLATFORM |
| 65 |
when /linux/ |
| 66 |
case error.message |
| 67 |
when /\A(\/.+?): (?:invalid ELF header|file too short)/ |
| 68 |
# This may be a linker script: |
| 69 |
# https://sourceware.org/binutils/docs/ld.html#Scripts |
| 70 |
path = $1 |
| 71 |
else |
| 72 |
raise |
| 73 |
end |
| 74 |
else |
| 75 |
raise |
| 76 |
end |
| 77 |
|
| 78 |
File.open(path) do |input| |
| 79 |
input.each_line do |line| |
| 80 |
case line |
| 81 |
when /\A\s*(?:INPUT|GROUP)\s*\(\s*([^\s,\)]+)/ |
| 82 |
# TODO: Should we support multiple files? |
| 83 |
return dlopen($1) |
| 84 |
end |
| 85 |
end |
| 86 |
end |
| 87 |
|
| 88 |
# Not found |
| 89 |
raise |
| 90 |
end |
| 91 |
end |
| 92 |
module_function :dlopen |
| 93 |
|
| 94 |
# Add constants for backwards compat |
| 95 |
|
| 96 |
RTLD_GLOBAL = Handle::RTLD_GLOBAL # :nodoc: |
| 97 |
RTLD_LAZY = Handle::RTLD_LAZY # :nodoc: |
| 98 |
RTLD_NOW = Handle::RTLD_NOW # :nodoc: |
| 99 |
|
| 100 |
Fiddle::Types.constants.each do |type| |
| 101 |
const_set "TYPE_#{type}", Fiddle::Types.const_get(type) |
| 102 |
end |
| 103 |
end |