Skip to content

Instantly share code, notes, and snippets.

@detunized
Created January 18, 2018 10:17
Show Gist options
  • Save detunized/f654b34b4918a54676b6c0267f2761d7 to your computer and use it in GitHub Desktop.
Save detunized/f654b34b4918a54676b6c0267f2761d7 to your computer and use it in GitHub Desktop.
Find which library (.a or .so) exports a symbol
#!/usr/bin/env ruby
begin
require "parallel"
PARALLEL = true
rescue LoadError
PARALLEL = false
end
def each collection, &block
if PARALLEL
Parallel.each collection, &block
else
collection.each &block
end
end
def usage
puts "find-lib.rb path symbol"
puts "Example: find-lib.rb /usr/lib printf"
exit 1
end
dir = ARGV[0] or usage
what = ARGV[1] or usage
# TODO: Make this configurable
what = what.downcase
Dir.chdir dir do
each Dir["**/*.{a,so}"] do |lib|
found = `nm --extern-only --demangle '#{lib}' 2>/dev/null`
.split("\n")
.map { |i| i[/^[0-9a-f]+ (. .*)/i, 1] }
.compact
.select { |i| i.downcase.include? what }
if ! found.empty?
puts lib
puts found.map { |i| " - #{i}" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment