Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import glob |
| 4 | import os |
| 5 | import re |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 6 | import subprocess |
| 7 | import sys |
| 8 | |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 9 | only_unwanted = False |
| 10 | if len(sys.argv) > 1: |
| 11 | if sys.argv[1] in ('-u', '--unwanted'): |
| 12 | only_unwanted = True |
| 13 | |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 14 | toolchain = os.environ['ANDROID_TOOLCHAIN'] |
| 15 | arch = re.sub(r'.*/linux-x86/([^/]+)/.*', r'\1', toolchain) |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 16 | if arch == 'aarch64': |
| 17 | arch = 'arm64' |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 6370aed | 2014-11-05 16:22:26 -0800 | [diff] [blame^] | 19 | def GetSymbolsFromTxt(txt_file): |
| 20 | symbols = set() |
| 21 | f = open(txt_file, 'r') |
| 22 | for line in f.read().splitlines(): |
| 23 | symbols.add(line) |
| 24 | f.close() |
| 25 | return symbols |
| 26 | |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 27 | def GetSymbolsFromSo(so_file): |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 28 | # Example readelf output: |
| 29 | # 264: 0001623c 4 FUNC GLOBAL DEFAULT 8 cabsf |
| 30 | # 266: 00016244 4 FUNC GLOBAL DEFAULT 8 dremf |
| 31 | # 267: 00019018 4 OBJECT GLOBAL DEFAULT 11 __fe_dfl_env |
| 32 | # 268: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_dcmplt |
| 33 | |
| 34 | r = re.compile(r' +\d+: [0-9a-f]+ +\d+ (I?FUNC|OBJECT) +\S+ +\S+ +\d+ (\S+)') |
| 35 | |
| 36 | symbols = set() |
| 37 | |
| 38 | for line in subprocess.check_output(['readelf', '--dyn-syms', '-W', so_file]).split('\n'): |
Elliott Hughes | e8e4534 | 2014-06-13 11:50:07 -0700 | [diff] [blame] | 39 | if ' HIDDEN ' in line or ' UND ' in line: |
| 40 | continue |
| 41 | m = r.match(line) |
| 42 | if m: |
| 43 | symbol = m.group(2) |
| 44 | symbol = re.sub('@.*', '', symbol) |
| 45 | symbols.add(symbol) |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 46 | |
| 47 | return symbols |
| 48 | |
| 49 | def GetSymbolsFromAndroidSo(*files): |
| 50 | symbols = set() |
| 51 | for f in files: |
| 52 | symbols = symbols | GetSymbolsFromSo('%s/system/lib64/%s' % (os.environ['ANDROID_PRODUCT_OUT'], f)) |
| 53 | return symbols |
| 54 | |
| 55 | def GetSymbolsFromSystemSo(*files): |
| 56 | symbols = set() |
| 57 | for f in files: |
| 58 | f = glob.glob('/lib/x86_64-linux-gnu/%s' % f)[-1] |
| 59 | symbols = symbols | GetSymbolsFromSo(f) |
| 60 | return symbols |
| 61 | |
Elliott Hughes | e8e4534 | 2014-06-13 11:50:07 -0700 | [diff] [blame] | 62 | def MangleGlibcNameToBionic(name): |
| 63 | if name in glibc_to_bionic_names: |
| 64 | return glibc_to_bionic_names[name] |
| 65 | return name |
| 66 | |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 67 | def GetNdkIgnored(): |
| 68 | global arch |
| 69 | symbols = set() |
| 70 | files = glob.glob('%s/ndk/build/tools/unwanted-symbols/%s/*' % |
| 71 | (os.getenv('ANDROID_BUILD_TOP'), arch)) |
| 72 | for f in files: |
| 73 | symbols |= set(open(f, 'r').read().splitlines()) |
| 74 | return symbols |
| 75 | |
Elliott Hughes | e8e4534 | 2014-06-13 11:50:07 -0700 | [diff] [blame] | 76 | glibc_to_bionic_names = { |
| 77 | '__res_init': 'res_init', |
| 78 | '__res_mkquery': 'res_mkquery', |
| 79 | '__res_query': 'res_query', |
| 80 | '__res_search': 'res_search', |
Dan Albert | 2320b02 | 2014-08-21 11:36:07 -0700 | [diff] [blame] | 81 | '__xpg_basename': '__gnu_basename', |
Elliott Hughes | e8e4534 | 2014-06-13 11:50:07 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 84 | glibc = GetSymbolsFromSystemSo('libc.so.*', 'librt.so.*', 'libpthread.so.*', 'libresolv.so.*', 'libm.so.*') |
| 85 | bionic = GetSymbolsFromAndroidSo('libc.so', 'libm.so') |
Elliott Hughes | 6370aed | 2014-11-05 16:22:26 -0800 | [diff] [blame^] | 86 | posix = GetSymbolsFromTxt(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'posix-2013.txt')) |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 87 | ndk_ignored = GetNdkIgnored() |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 88 | |
Elliott Hughes | e8e4534 | 2014-06-13 11:50:07 -0700 | [diff] [blame] | 89 | glibc = map(MangleGlibcNameToBionic, glibc) |
| 90 | |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 91 | # bionic includes various BSD symbols to ease porting other BSD-licensed code. |
| 92 | bsd_stuff = set([ |
Elliott Hughes | 45bf4c3 | 2014-05-22 18:53:21 -0700 | [diff] [blame] | 93 | 'basename_r', |
| 94 | 'dirname_r', |
| 95 | 'fgetln', |
| 96 | 'fpurge', |
| 97 | 'funopen', |
| 98 | 'gamma_r', |
| 99 | 'gammaf_r', |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 100 | 'getprogname', |
| 101 | 'setprogname', |
| 102 | 'strlcat', |
| 103 | 'strlcpy', |
Elliott Hughes | 45bf4c3 | 2014-05-22 18:53:21 -0700 | [diff] [blame] | 104 | 'sys_signame', |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 105 | 'wcslcat', |
| 106 | 'wcslcpy' |
| 107 | ]) |
| 108 | # Some symbols are part of the FORTIFY implementation. |
| 109 | FORTIFY_stuff = set([ |
| 110 | '__FD_CLR_chk', |
| 111 | '__FD_ISSET_chk', |
| 112 | '__FD_SET_chk', |
| 113 | '__stack_chk_guard', |
| 114 | '__stpncpy_chk2', |
| 115 | '__strchr_chk', |
| 116 | '__strlcat_chk', |
| 117 | '__strlcpy_chk', |
| 118 | '__strlen_chk', |
| 119 | '__strncpy_chk2', |
| 120 | '__strrchr_chk', |
| 121 | '__umask_chk' |
| 122 | ]) |
Elliott Hughes | b497c43 | 2014-05-20 20:37:56 -0700 | [diff] [blame] | 123 | # Some symbols are used to implement public macros. |
| 124 | macro_stuff = set([ |
| 125 | '__assert2', |
| 126 | '__errno', |
| 127 | '__fe_dfl_env', |
| 128 | '__get_h_errno', |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 129 | '__fpclassifyd', |
| 130 | '__isfinite', |
| 131 | '__isfinitef', |
| 132 | '__isfinitel', |
| 133 | '__isnormal', |
| 134 | '__isnormalf', |
| 135 | '__isnormall', |
| 136 | '__sF', |
| 137 | '__pthread_cleanup_pop', |
| 138 | '__pthread_cleanup_push', |
Elliott Hughes | b497c43 | 2014-05-20 20:37:56 -0700 | [diff] [blame] | 139 | ]) |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 140 | # bionic exposes various Linux features that glibc doesn't. |
| 141 | linux_stuff = set([ |
| 142 | 'getauxval', |
| 143 | 'gettid', |
| 144 | 'tgkill' |
| 145 | ]) |
| 146 | # Some standard stuff isn't yet in the versions of glibc we're using. |
| 147 | std_stuff = set([ |
Elliott Hughes | f6b1d43 | 2014-06-06 15:20:50 -0700 | [diff] [blame] | 148 | 'at_quick_exit', |
| 149 | 'c16rtomb', |
| 150 | 'c32rtomb', |
| 151 | 'mbrtoc16', |
| 152 | 'mbrtoc32', |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 153 | ]) |
| 154 | # These have mangled names in glibc, with a macro taking the "obvious" name. |
| 155 | weird_stuff = set([ |
| 156 | 'fstat', |
| 157 | 'fstat64', |
| 158 | 'fstatat', |
| 159 | 'fstatat64', |
| 160 | 'isfinite', |
| 161 | 'isfinitef', |
| 162 | 'isfinitel', |
| 163 | 'isnormal', |
| 164 | 'isnormalf', |
| 165 | 'isnormall', |
| 166 | 'lstat', |
| 167 | 'lstat64', |
| 168 | 'mknod', |
| 169 | 'mknodat', |
| 170 | 'stat', |
| 171 | 'stat64', |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 172 | 'optreset', |
| 173 | 'sigsetjmp', |
| 174 | ]) |
| 175 | # These exist in glibc, but under slightly different names (generally one extra |
| 176 | # or one fewer _). TODO: check against glibc names. |
| 177 | libresolv_stuff = set([ |
| 178 | '__res_send_setqhook', |
| 179 | '__res_send_setrhook', |
| 180 | '_resolv_flush_cache_for_net', |
| 181 | '_resolv_set_nameservers_for_net', |
| 182 | 'dn_expand', |
| 183 | 'nsdispatch', |
| 184 | ]) |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 185 | # Implementation details we know we export (and can't get away from). |
| 186 | known = set([ |
| 187 | '_ctype_', |
| 188 | '__libc_init', |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 189 | ]) |
| 190 | |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 191 | if not only_unwanted: |
| 192 | print 'glibc:' |
| 193 | for symbol in sorted(glibc): |
| 194 | print symbol |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 195 | |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 196 | print |
| 197 | print 'bionic:' |
| 198 | for symbol in sorted(bionic): |
| 199 | print symbol |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 200 | |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 201 | print |
Elliott Hughes | 6370aed | 2014-11-05 16:22:26 -0800 | [diff] [blame^] | 202 | print 'in posix but not bionic:' |
| 203 | for symbol in sorted(posix.difference(bionic)): |
| 204 | print symbol |
| 205 | |
| 206 | print |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 207 | print 'in bionic but not glibc:' |
| 208 | |
| 209 | allowed_stuff = (bsd_stuff | FORTIFY_stuff | linux_stuff | macro_stuff | |
Dan Albert | fd5ee9a | 2014-08-15 14:20:04 -0700 | [diff] [blame] | 210 | std_stuff | weird_stuff | libresolv_stuff | known) |
Elliott Hughes | b497c43 | 2014-05-20 20:37:56 -0700 | [diff] [blame] | 211 | for symbol in sorted((bionic - allowed_stuff).difference(glibc)): |
Dan Albert | 76212ee | 2014-08-13 13:04:28 -0700 | [diff] [blame] | 212 | if symbol in ndk_ignored: |
| 213 | symbol += '*' |
Elliott Hughes | 5a93e88 | 2014-05-16 14:44:38 -0700 | [diff] [blame] | 214 | print symbol |
| 215 | |
| 216 | sys.exit(0) |