1#!/usr/bin/env python3
2
3import sys, os, shutil, subprocess, glob, re
4
5builddir = os.getenv ('builddir', os.path.dirname (__file__))
6libs = os.getenv ('libs', '.libs')
7
8objdump = shutil.which ('objdump')
9if not objdump:
10	print ('check-static-inits.py: \'ldd\' not found; skipping test')
11	sys.exit (77)
12
13if sys.version_info < (3, 5):
14	print ('check-static-inits.py: needs python 3.5 for recursive support in glob')
15	sys.exit (77)
16
17OBJS = glob.glob (os.path.join (builddir, libs, '**', '*.o'), recursive=True)
18if not OBJS:
19	print ('check-static-inits.py: object files not found; skipping test')
20	sys.exit (77)
21
22stat = 0
23
24for obj in OBJS:
25	result = subprocess.check_output ([objdump, '-t', obj]).decode ('utf-8')
26
27	# Checking that no object file has static initializers
28	for l in re.findall (r'^.*\.[cd]tors.*$', result, re.MULTILINE):
29		if not re.match (r'.*\b0+\b', l):
30			print ('Ouch, %s has static initializers/finalizers' % obj)
31			stat = 1
32
33	# Checking that no object file has lazy static C++ constructors/destructors or other such stuff
34	if ('__cxa_' in result) and ('__ubsan_handle' not in result):
35		print ('Ouch, %s has lazy static C++ constructors/destructors or other such stuff' % obj)
36		stat = 1
37
38sys.exit (stat)
39