1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2022 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import string 20import json 21import sys 22import os 23import struct 24 25# find out/rk3568/packages/phone/system/ -type f -print | file -f - | grep ELF | cut -d":" -f1 | wc -l 26 27class ELFWalker(): 28 def __init__(self, product_out_path="/home/z00325844/demo/archinfo/assets/rk3568/3.2.7.5"): 29 self._files = [] 30 self._links = {} 31 self._walked = False 32 self._product_out_path = product_out_path 33 34 def get_product_images_path(self): 35 return os.path.join(self._product_out_path, "packages/phone/") 36 37 def get_product_out_path(self): 38 return self._product_out_path 39 40 def __walk_path(self, subdir): 41 for root, subdirs, files in os.walk(os.path.join(self._product_out_path, subdir)): 42 for _filename in files: 43 _assetFile = os.path.join(root, _filename) 44 if os.path.islink(_assetFile): 45 if _assetFile.find(".so") > 0: 46 target = os.readlink(_assetFile) 47 self._links[_assetFile] = target 48 continue 49 if not os.path.isfile(_assetFile): 50 continue 51 with open(_assetFile, "rb") as f: 52 data = f.read(4) 53 try: 54 magic = struct.unpack("Bccc", data) 55 if magic[0] == 0x7F and magic[1] == b'E' and magic[2] == b'L' and magic[3] == b'F': 56 self._files.append(_assetFile) 57 except: 58 pass 59 60 self._walked = True 61 62 def get_link_file_map(self): 63 if not self._walked: 64 self.__walk_path("packages/phone/system") 65 self.__walk_path("packages/phone/vendor") 66 return self._links 67 68 def get_elf_files(self): 69 if not self._walked: 70 self.__walk_path("packages/phone/system") 71 self.__walk_path("packages/phone/vendor") 72 return self._files 73 74if __name__ == '__main__': 75 elfFiles = ELFWalker() 76 for f in elfFiles.get_elf_files(): 77 print(f) 78 for src, target in elfFiles.get_link_file_map().items(): 79 print('{} -> {}'.format(str, target)) 80