2 # Copyright (c) 2017 Google Inc.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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 """Checks names of global exports from a library."""
17 from __future__ import print_function
25 PROG = 'check_symbol_exports'
28 def command_output(cmd, directory):
29 """Runs a command in a directory and returns its standard output stream.
31 Captures the standard error stream.
33 Raises a RuntimeError if the command fails to launch or otherwise fails.
35 p = subprocess.Popen(cmd,
37 stdout=subprocess.PIPE,
38 stderr=subprocess.PIPE)
39 (stdout, _) = p.communicate()
41 raise RuntimeError('Failed to run %s in %s' % (cmd, directory))
45 def check_library(library):
46 """Scans the given library file for global exports. If all such
47 exports are namespaced or begin with spv (in either C or C++ styles)
48 then return 0. Otherwise emit a message and return 1."""
50 # The pattern for a global symbol record
51 symbol_pattern = re.compile(r'^[0-aA-Fa-f]+ g *F \.text.*[0-9A-Fa-f]+ +(.*)')
53 # Ok patterns are as follows, assuming Itanium name mangling:
54 # spv[A-Z] : extern "C" symbol starting with spv
55 # _ZN : something in a namespace
56 # _Z[0-9]+spv[A-Z_] : C++ symbol starting with spv[A-Z_]
57 symbol_ok_pattern = re.compile(r'^(spv[A-Z]|_ZN|_Z[0-9]+spv[A-Z_])')
60 for line in command_output(['objdump', '-t', library], '.').split('\n'):
61 match = symbol_pattern.search(line)
63 symbol = match.group(1)
64 if symbol not in seen:
66 #print("look at '{}'".format(symbol))
67 if not symbol_ok_pattern.match(symbol):
68 print('{}: error: Unescaped exported symbol: {}'.format(PROG, symbol))
75 parser = argparse.ArgumentParser(description='Check global names exported from a library')
76 parser.add_argument('library', help='The static library to examine')
77 args = parser.parse_args()
79 if not os.path.isfile(args.library):
80 print('{}: error: {} does not exist'.format(PROG, args.library))
83 if os.name is 'posix':
84 status = check_library(args.library)
87 print('Passing test since not on Posix')
91 if __name__ == '__main__':