1# Copyright 2018 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import os 6import subprocess 7 8from whitelist import is_whitelisted 9 10def check_debug_info(dso_path, readelf_content): 11 """check whether debug info section exists in the elf file. 12 13 Args: 14 readelf: debug info dumped by command readelf 15 16 Returns: 17 True if debug info section exists, otherwise False. 18 """ 19 20 # Return True if it is whitelisted 21 if is_whitelisted('exist_debug_info', dso_path): 22 return True 23 24 for l in readelf_content: 25 if 'debug_info' in l: 26 return True 27 return False 28 29def check_producer(dso_path, readelf_content): 30 """check whether DW_AT_producer exists in each compile unit. 31 32 Args: 33 readelf: debug info dumped by command readelf 34 35 Returns: 36 True if DW_AT_producer exists in each compile unit, otherwise False. 37 Notice: If no compile unit in DSO, also return True. 38 """ 39 40 # Return True if it is whitelisted 41 if is_whitelisted('exist_producer', dso_path): 42 return True 43 44 # Indicate if there is a producer under each cu 45 cur_producer = False 46 47 first_cu = True 48 producer_exist = True 49 50 for l in readelf_content: 51 if 'DW_TAG_compile_unit' in l: 52 if not first_cu and not cur_producer: 53 producer_exist = False 54 break 55 first_cu = False 56 cur_producer = False 57 elif 'DW_AT_producer' in l: 58 cur_producer = True 59 60 # Check whether last producer of compile unit exists in the elf, 61 # also return True if no cu in the DSO. 62 if not first_cu and not cur_producer: 63 producer_exist = False 64 65 return producer_exist 66 67def check_exist_all(dso_path): 68 """check whether intended components exists in the given dso. 69 70 Args: 71 dso_path: path to the dso 72 Return: 73 True if everything looks fine otherwise False. 74 """ 75 76 readelf = subprocess.Popen(['readelf', '--debug-dump=info', 77 '--dwarf-depth=1', dso_path], 78 stdout=subprocess.PIPE, 79 stderr=open(os.devnull, 'w')) 80 readelf_content = list(readelf.stdout) 81 82 exist_checks = [check_debug_info, check_producer] 83 84 for e in exist_checks: 85 if not e(dso_path, readelf_content): 86 check_failed = e.__module__ + ': ' + e.__name__ 87 print('%s failed check: %s' % (dso_path, check_failed)) 88 return False 89 90 return True 91