1# -*- coding: utf-8 -*- 2# Copyright 2018 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""check compile units.""" 7 8from __future__ import print_function 9 10import os 11import subprocess 12 13import check_ngcc 14 15cu_checks = [check_ngcc.not_by_gcc] 16 17 18def check_compile_unit(dso_path, producer, comp_path): 19 """check all compiler flags used to build the compile unit. 20 21 Args: 22 dso_path: path to the elf/dso. 23 producer: DW_AT_producer contains the compiler command line. 24 comp_path: DW_AT_comp_dir + DW_AT_name. 25 26 Returns: 27 A set of failed tests. 28 """ 29 failed = set() 30 for c in cu_checks: 31 if not c(dso_path, producer, comp_path): 32 failed.add(c.__module__) 33 34 return failed 35 36 37def check_compile_units(dso_path): 38 """check all compile units in the given dso. 39 40 Args: 41 dso_path: path to the dso. 42 43 Returns: 44 True if everything looks fine otherwise False. 45 """ 46 47 failed = set() 48 producer = '' 49 comp_path = '' 50 51 readelf = subprocess.Popen( 52 ['llvm-dwarfdump', '--recurse-depth=0', dso_path], 53 stdout=subprocess.PIPE, 54 stderr=open(os.devnull, 'w'), 55 encoding='utf-8') 56 for l in readelf.stdout: 57 if 'DW_TAG_compile_unit' in l: 58 if producer: 59 failed = failed.union(check_compile_unit(dso_path, producer, comp_path)) 60 producer = '' 61 comp_path = '' 62 elif 'DW_AT_producer' in l: 63 producer = l 64 elif 'DW_AT_name' in l: 65 comp_path = os.path.join(comp_path, l.split(':')[-1].strip()) 66 elif 'DW_AT_comp_dir' in l: 67 comp_path = os.path.join(l.split(':')[-1].strip(), comp_path) 68 if producer: 69 failed = failed.union(check_compile_unit(dso_path, producer, comp_path)) 70 71 if failed: 72 print('%s failed check: %s' % (dso_path, ' '.join(failed))) 73 return False 74 75 return True 76