1#!/usr/bin/env python3 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 sys 20import argparse 21import os 22import platform 23 24 25def create_mini_debug_info(binary_path, stripped_binary_path, root_path): 26 # temporary file path 27 dynsyms_path = stripped_binary_path + ".dynsyms" 28 funcsysms_path = stripped_binary_path + ".funcsyms" 29 keep_path = stripped_binary_path + ".keep" 30 debug_path = stripped_binary_path + ".debug" 31 mini_debug_path = stripped_binary_path + ".minidebug" 32 33 # llvm tools path 34 host_platform = platform.uname().system.lower() 35 host_cpu = platform.uname().machine.lower() 36 llvm_dir_path = os.path.join( 37 root_path, 'prebuilts/clang/ohos', host_platform + '-' + host_cpu, 'llvm/bin') 38 llvm_nm_path = os.path.join(llvm_dir_path, "llvm-nm") 39 llvm_objcopy_path = os.path.join(llvm_dir_path, "llvm-objcopy") 40 41 cmd_list = [] 42 43 gen_symbols_cmd = llvm_nm_path + " -D " + binary_path + \ 44 " --format=posix --defined-only | awk \'{ print $1}\' | sort > " + dynsyms_path 45 gen_func_symbols_cmd = llvm_nm_path + " " + binary_path + \ 46 " --format=posix --defined-only | awk \'{ if ($2 == \"t\" || $2== \"T\" || $2 == \"d\" ) print $1}\' | sort > " + funcsysms_path 47 gen_keep_symbols_cmd = "comm -13 " + dynsyms_path + \ 48 " " + funcsysms_path + " > " + keep_path 49 gen_keep_debug_cmd = llvm_objcopy_path + \ 50 " --only-keep-debug " + binary_path + " " + debug_path 51 gen_mini_debug_cmd = llvm_objcopy_path + " -S --remove-section .gbd_index --remove-section .comment --keep-symbols=" + \ 52 keep_path + " " + debug_path + " " + mini_debug_path 53 compress_debuginfo = "xz " + mini_debug_path 54 gen_stripped_binary = llvm_objcopy_path + " --add-section .gnu_debugdata=" + \ 55 mini_debug_path + ".xz " + stripped_binary_path 56 57 cmd_list.append(gen_symbols_cmd) 58 cmd_list.append(gen_func_symbols_cmd) 59 cmd_list.append(gen_keep_symbols_cmd) 60 cmd_list.append(gen_keep_debug_cmd) 61 cmd_list.append(gen_mini_debug_cmd) 62 cmd_list.append(compress_debuginfo) 63 cmd_list.append(gen_stripped_binary) 64 65 # execute each cmd to generate temporary file 66 # which .gnu_debugdata section depends on 67 for cmd in cmd_list: 68 os.system(cmd) 69 70 # remove temporary file 71 os.remove(dynsyms_path) 72 os.remove(funcsysms_path) 73 os.remove(keep_path) 74 os.remove(debug_path) 75 os.remove(mini_debug_path + ".xz") 76 77 78def main(): 79 parser = argparse.ArgumentParser(description=__doc__) 80 parser.add_argument("--unstripped-path", 81 help="unstripped binary path") 82 parser.add_argument("--stripped-path", 83 help="stripped binary path") 84 parser.add_argument("--root-path", 85 help="root path is used to search llvm toolchain") 86 args = parser.parse_args() 87 88 create_mini_debug_info(args.unstripped_path, 89 args.stripped_path, args.root_path) 90 91 92if __name__ == "__main__": 93 sys.exit(main()) 94