2020-12-23 06:16:18 +03:00
|
|
|
#
|
2020-12-23 06:18:41 +03:00
|
|
|
# Listing C's global variables in .so or .o, or .bundle on Mac OS using "objdump -t" (elf64-x86-64)
|
2020-12-23 06:16:18 +03:00
|
|
|
# to check ractor-safety.
|
|
|
|
#
|
2020-12-23 06:18:41 +03:00
|
|
|
# Usage: ruby search-cgvars.rb foo.so bar.o .ext/x86_64-darwin18/psych.bundle
|
2020-12-23 06:16:18 +03:00
|
|
|
#
|
2020-12-23 06:18:41 +03:00
|
|
|
MAC_OS = RbConfig::CONFIG['host_os'].match? /darwin|mac os/
|
|
|
|
|
2020-12-23 06:16:18 +03:00
|
|
|
def gvars file
|
|
|
|
# '0000000000031ac8 g O .bss 0000000000000008 rb_cSockIfaddr'
|
2020-12-23 06:18:41 +03:00
|
|
|
# On mac, with .bundle files:
|
|
|
|
# '0000000000004258 l O __DATA,__bss _passwd_blocking'
|
|
|
|
|
2020-12-23 06:16:18 +03:00
|
|
|
strs = `objdump -t #{file}`
|
|
|
|
found = {}
|
|
|
|
strs.each_line{|line|
|
|
|
|
if /[\da-f]{16} / =~ line
|
|
|
|
addr = line[0...16]
|
|
|
|
flags = line[17...24].tr(' ', '').split(//).sort.uniq
|
|
|
|
rest = line[25..]
|
2020-12-23 06:18:41 +03:00
|
|
|
if MAC_OS
|
|
|
|
seg, name = rest.split(/\s+/)
|
|
|
|
else
|
|
|
|
seg, size, name = rest.split(/\s+/)
|
|
|
|
end
|
2020-12-23 06:16:18 +03:00
|
|
|
if flags.include?('O')
|
|
|
|
# p [addr, flags, seg, size, name]
|
2020-12-23 06:18:41 +03:00
|
|
|
found[name] = [flags, seg, *size]
|
2020-12-23 06:16:18 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
puts "## #{file}:"
|
|
|
|
found.sort_by{|name, (flags, *)|
|
|
|
|
[flags, name]
|
|
|
|
}.each{|name, rest|
|
|
|
|
flags, seg, size = *rest
|
2020-12-23 06:18:41 +03:00
|
|
|
next if (size.to_i == 0 && !MAC_OS) && seg != '*UND*'
|
2020-12-23 06:16:18 +03:00
|
|
|
case seg
|
|
|
|
when ".rodata", ".data.rel.ro", ".got.plt", ".eh_frame", ".fini_array"
|
|
|
|
next
|
2020-12-23 06:18:41 +03:00
|
|
|
when /,__const$/ # Mac OS
|
|
|
|
next
|
2020-12-23 06:16:18 +03:00
|
|
|
end
|
|
|
|
case name
|
|
|
|
when /^id_/, /^rbimpl_id/, /^sym_/, /^rb_[cme]/, /\Acompleted\.\d+\z/
|
|
|
|
next
|
2020-12-23 06:18:41 +03:00
|
|
|
when /^_id_/, /\.rbimpl_id(\.\d+)?$/ # Mac OS
|
|
|
|
next
|
2020-12-23 06:16:18 +03:00
|
|
|
end
|
|
|
|
puts " %40s %s" % [name, rest.inspect]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
ARGV.each{|file|
|
|
|
|
gvars file
|
|
|
|
}
|