blob: 43454e4914c017f5aad0748fbc0a4c097dea1c2b [file] [log] [blame]
Dan Albert169eb662015-01-21 16:42:02 -08001#
2# Copyright (C) 2015 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the 'License');
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an 'AS IS' BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import glob
17import os
18import re
19import subprocess
20
21
22def GetFromTxt(txt_file):
23 symbols = set()
24 f = open(txt_file, 'r')
25 for line in f.read().splitlines():
26 symbols.add(line)
27 f.close()
28 return symbols
29
30
31def GetFromSo(so_file):
32 # pylint: disable=line-too-long
33 # Example readelf output:
34 # 264: 0001623c 4 FUNC GLOBAL DEFAULT 8 cabsf
35 # 266: 00016244 4 FUNC GLOBAL DEFAULT 8 dremf
36 # 267: 00019018 4 OBJECT GLOBAL DEFAULT 11 __fe_dfl_env
37 # 268: 00000000 0 FUNC GLOBAL DEFAULT UND __aeabi_dcmplt
38
39 r = re.compile(
40 r' +\d+: [0-9a-f]+ +\d+ (I?FUNC|OBJECT) +\S+ +\S+ +\d+ (\S+)')
41
42 symbols = set()
43
44 output = subprocess.check_output(['readelf', '--dyn-syms', '-W', so_file])
45 for line in output.split('\n'):
46 if ' HIDDEN ' in line or ' UND ' in line:
47 continue
48 m = r.match(line)
49 if m:
50 symbol = m.group(2)
51 symbol = re.sub('@.*', '', symbol)
52 symbols.add(symbol)
53
54 return symbols
55
56
57def GetFromAndroidSo(files):
58 out_dir = os.environ['ANDROID_PRODUCT_OUT']
59 lib_dir = os.path.join(out_dir, 'system/lib64')
60 if not os.path.isdir(lib_dir):
61 lib_dir = os.path.join(out_dir, 'system/lib')
62
63 results = set()
64 for f in files:
65 results |= GetFromSo(os.path.join(lib_dir, f))
66 return results
67
68
69def GetFromSystemSo(files):
70 lib_dir = '/lib/x86_64-linux-gnu'
71 results = set()
72 for f in files:
73 results |= GetFromSo(glob.glob(os.path.join(lib_dir, f))[-1])
74 return results