Skip to content

Commit

Permalink
os/linux/elf: avoid using ldd for listing dynamic dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alebcay committed Mar 23, 2024
1 parent 9a1793a commit 63bb563
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions Library/Homebrew/os/linux/elf.rb
Expand Up @@ -130,19 +130,7 @@ def initialize(path)
@dylib_id, needed = needed_libraries path
return if needed.empty?

ldd = DevelopmentTools.locate "ldd"
ldd_output = Utils.popen_read(ldd, path.expand_path.to_s).split("\n")
return unless $CHILD_STATUS.success?

ldd_paths = ldd_output.filter_map do |line|
match = line.match(/\t.+ => (.+) \(.+\)|\t(.+) => not found/)
next unless match

match.captures.compact.first
end
@dylibs = ldd_paths.select do |ldd_path|
needed.include? File.basename(ldd_path)
end
@dylibs = needed.map { |lib| find_full_lib_path(lib).to_s }
end

private
Expand All @@ -157,6 +145,49 @@ def needed_libraries_using_patchelf_rb(path)
patcher = path.patchelf_patcher
[patcher.soname, patcher.needed]
end

def ld_so_conf(conf_path = "/etc/ld.so.conf")
conf_file = Pathname(conf_path)
paths = Set.new
directory = conf_file.realpath.dirname

conf_file.readlines.each do |line|
line.rstrip!
if line.start_with?(/\s*include\s+/)
include_path = Pathname(line.sub(/^\s*include\s+/, "")).expand_path
wildcard = include_path.absolute? ? include_path : directory/include_path

Dir.glob(wildcard.to_s).each do |include_file|
paths += ld_so_conf(include_file)
end
elsif line.start_with?(/\s*#/) || line.empty?
next
end

paths << Pathname(line)
end

paths.to_a
end

def find_full_lib_path(basename)
local_paths = (path.patchelf_patcher.runpath || path.patchelf_patcher.rpath)&.split(":")&.map do |p|
Pathname(p)
end

local_paths&.each do |local_path|
candidate = local_path/basename
return candidate if candidate.exist? && candidate.elf?
end

global_paths = ld_so_conf
global_paths.each do |global_path|
candidate = global_path/basename
return candidate if candidate.exist? && candidate.elf?
end

basename

Check warning on line 189 in Library/Homebrew/os/linux/elf.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/os/linux/elf.rb#L189

Added line #L189 was not covered by tests
end
end
private_constant :Metadata

Expand Down

0 comments on commit 63bb563

Please sign in to comment.