2 # Listing C's global variables in .so or .o, or .bundle on Mac OS using "objdump -t" (elf64-x86-64)
3 # to check ractor-safety.
5 # Usage: ruby search-cgvars.rb foo.so bar.o .ext/x86_64-darwin18/psych.bundle
7 MAC_OS = RbConfig::CONFIG['host_os'].match? /darwin|mac os/
10 # '0000000000031ac8 g O .bss 0000000000000008 rb_cSockIfaddr'
11 # On mac, with .bundle files:
12 # '0000000000004258 l O __DATA,__bss _passwd_blocking'
14 strs = `objdump -t #{file}`
17 if /[\da-f]{16} / =~ line
19 flags = line[17...24].tr(' ', '').split(//).sort.uniq
22 seg, name = rest.split(/\s+/)
24 seg, size, name = rest.split(/\s+/)
26 if flags.include?('O')
27 # p [addr, flags, seg, size, name]
28 found[name] = [flags, seg, *size]
33 found.sort_by{|name, (flags, *)|
36 flags, seg, size = *rest
37 next if (size.to_i == 0 && !MAC_OS) && seg != '*UND*'
39 when ".rodata", ".data.rel.ro", ".got.plt", ".eh_frame", ".fini_array"
41 when /,__const$/ # Mac OS
45 when /^id_/, /^rbimpl_id/, /^sym_/, /^rb_[cme]/, /\Acompleted\.\d+\z/
47 when /^_id_/, /\.rbimpl_id(\.\d+)?$/ # Mac OS
50 puts " %40s %s" % [name, rest.inspect]