module Elf::Utilities

Public Class Methods

append_to_library_path(morepaths) click to toggle source

Convenience function to append an array to the list of system library paths.

This is just used to avoid repeating the same block of code for both the data read from /etc/ld.so.conf and from LD_LIBRARY_PATH environment variable if requested.

# File lib/elf/utils/loader.rb, line 34
def self.append_to_library_path(morepaths)
  morepaths.each do |path|
    begin
      # Since we can have symlinks and similar issues, try to
      # canonicalise the paths so that we can expect them to be
      # truly unique (and thus never pass through the same directory
      # twice).
      @@system_library_path << Pathname.new(path).realpath.to_s
    rescue Errno::ENOENT, Errno::EACCES
    end
  end
end
environment_library_path() click to toggle source

Return the environment library path

We assume the LD_LIBRARY_PATH variable is not going to change between calls.

TODO: systems like Solaris implement further variables like LD_LIBRARY_PATH_32 and LD_LIBRARY_PATH_64, we should pay attention to those too.

# File lib/elf/utils/loader.rb, line 100
def self.environment_library_path
  return [] if ENV['LD_LIBRARY_PATH'].nil?

  ENV['LD_LIBRARY_PATH'].split(":").collect do |path|
    begin
      Pathname.new(path).realpath.to_s
    rescue Errno::ENOENT, Errno::EACCES
    end
  end.uniq
end
system_library_path() click to toggle source

Return the system library path to look for libraries, just like the loader would.

# File lib/elf/utils/loader.rb, line 49
def self.system_library_path

  # Try to cache the request since we most likely have multiple
  # request per process and we don't care if the settings change
  # between them.
  if @@system_library_path.nil?
    @@system_library_path = Array.new

    # We have to put by default /lib and /usr/lib since they are
    # implicit in all systems. In particular for Gentoo/Linux
    # these two are not in the list on x86 systems (but are on
    # amd64).
    #
    # Since LD_LIBRARY_PATH would win over this, but we expect
    # /etc/ld.so.conf not to, add them here.
    append_to_library_path(["/lib", "/usr/lib"])

    # We might not have the ld.so.conf file, if that's the case
    # just ignore it.
    begin
      # This implements for now the glibc-style loader
      # configuration; in the future it might be reimplemented to
      # take into consideration different operating systems.
      ::File.open("/etc/ld.so.conf") do |ld_so_conf|
        ld_so_conf.each_line do |line|
          # Comment lines in the configuration file are prefixed
          # with the hash character, and the remaining content is
          # just a single huge list of paths, separated by colon,
          # comma, space, tabs or newlines.
          append_to_library_path(line.gsub(/#.*/, '').split(/[:, \t\n]/))
        end
      end
    rescue Errno::ENOENT
    end

    # Make sure the resulting list is uniq to avoid scanning the
    # same directory multiple times.
    @@system_library_path.uniq!
  end
  
  return @@system_library_path
end