Skip to content

Commit

Permalink
Revert "os/linux/elf: avoid using ldd for listing dynamic dependencies"
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongRuoyu authored Apr 15, 2024
1 parent a553f96 commit 852c5ac
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 217 deletions.
62 changes: 13 additions & 49 deletions Library/Homebrew/os/linux/elf.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# typed: true
# frozen_string_literal: true

require "os/linux/ld"

# {Pathname} extension for dealing with ELF files.
# @see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header
#
Expand Down Expand Up @@ -132,7 +130,19 @@ def initialize(path)
@dylib_id, needed = needed_libraries path
return if needed.empty?

@dylibs = needed.map { |lib| find_full_lib_path(lib).to_s }
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
end

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

def find_full_lib_path(basename)
local_paths = (path.patchelf_patcher.runpath || path.patchelf_patcher.rpath)&.split(":")

# Search for dependencies in the runpath/rpath first
local_paths&.each do |local_path|
candidate = Pathname(local_path)/basename
return candidate if candidate.exist? && candidate.elf?
end

# Check if DF_1_NODEFLIB is set
dt_flags_1 = path.patchelf_patcher.elf.segment_by_type(:dynamic)&.tag_by_type(:flags_1)
nodeflib_flag = if dt_flags_1.nil?
false
else
dt_flags_1.value & ELFTools::Constants::DF::DF_1_NODEFLIB != 0
end

linker_library_paths = OS::Linux::Ld.library_paths
linker_system_dirs = OS::Linux::Ld.system_dirs

# If DF_1_NODEFLIB is set, exclude any library paths that are subdirectories
# of the system dirs
if nodeflib_flag
linker_library_paths = linker_library_paths.reject do |lib_path|
linker_system_dirs.any? { |system_dir| Utils::Path.child_of? system_dir, lib_path }
end
end

# If not found, search recursively in the paths listed in ld.so.conf (skipping
# paths that are subdirectories of the system dirs if DF_1_NODEFLIB is set)
linker_library_paths.each do |linker_library_path|
candidate = Pathname(linker_library_path)/basename
return candidate if candidate.exist? && candidate.elf?
end

# If not found, search in the system dirs, unless DF_1_NODEFLIB is set
unless nodeflib_flag
linker_system_dirs.each do |linker_system_dir|
candidate = Pathname(linker_system_dir)/basename
return candidate if candidate.exist? && candidate.elf?
end
end

basename
end
end
private_constant :Metadata

Expand Down
74 changes: 0 additions & 74 deletions Library/Homebrew/os/linux/ld.rb

This file was deleted.

47 changes: 0 additions & 47 deletions Library/Homebrew/test/os/linux/ld_spec.rb

This file was deleted.

33 changes: 0 additions & 33 deletions Library/Homebrew/test/utils/path_spec.rb

This file was deleted.

14 changes: 0 additions & 14 deletions Library/Homebrew/utils/path.rb

This file was deleted.

0 comments on commit 852c5ac

Please sign in to comment.