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 os 20from stat import * 21 22from .utils import command 23 24class ElfFile(dict): 25 def __init__(self, file, prefix): 26 self._f = file 27 self._f_safe = "'%s'" % file 28 29 self["name"] = os.path.basename(file) 30 self["size"] = os.stat(self._f)[ST_SIZE] 31 if self["name"].find(".so") > 0: 32 self["type"] = "lib" 33 else: 34 self["type"] = "bin" 35 self["path"] = file[len(prefix):] 36 37 def __eq__(self, other): 38 if not isinstance(other, ElfFile): 39 return NotImplemented 40 41 return self["path"] == other["path"]#and self["name"] == other["name"] 42 43 def __extract_soname(self): 44 soname_data = command("mklibs-readelf", "--print-soname", self._f_safe) 45 if soname_data: 46 return soname_data.pop() 47 return "" 48 49 def __extract_elf_size(self): 50 size_data = command("size", self._f_safe) 51 if not size_data or len(size_data) < 2: 52 self["text_size"] = 0 53 self["data_size"] = 0 54 self["bss_size"] = 0 55 return 0 56 57 vals = size_data[1].split() 58 self["text_size"] = int(vals[0]) 59 self["data_size"] = int(vals[1]) 60 self["bss_size"] = int(vals[2]) 61 return "" 62 63 def is_library(self): 64 if self["name"].find(".so") > 0: 65 return True 66 return False 67 68 def get_file(self): 69 return self._f 70 71 # Return a set of libraries the passed objects depend on. 72 def library_depends(self): 73 if not os.access(self._f, os.F_OK): 74 raise Exception("Cannot find lib: " + self._f) 75 dynamics = command("readelf", "--dynamic", self._f_safe) 76 res = [] 77 for line in dynamics: 78 pos = line.find("(NEEDED)") 79 if pos <= 0: 80 continue 81 line = line[pos + 8:] 82 line = line.strip() 83 if not line.startswith("Shared library:"): 84 continue 85 line = line[15:] 86 line = line.strip() 87 if line.startswith("["): 88 line = line[1:] 89 if line.endswith("]"): 90 line = line[:-1] 91 line = line.strip() 92 res.append(line) 93 return res 94 95 96if __name__ == '__main__': 97 import elf_walker 98 99 cnt = 0 100 elfFiles = elf_walker.ELFWalker() 101 for f in elfFiles.get_elf_files(): 102 if f.find("libskia_ohos.z.so") < 0: 103 continue 104 elf = ElfFile(f, elfFiles.get_product_images_path()) 105 print(f) 106