def complete_symbol(**kwargs): prefix = kwargs["prefix"] if prefix == "": # skip reading the map file, which would # result in a lot of useless completions return [] parsed_args = kwargs["parsed_args"] config = {} diff_settings.apply(config, parsed_args) mapfile = config.get("mapfile") if not mapfile: return [] completes = [] with open(mapfile) as f: data = f.read() # assume symbols are prefixed by a space character search = f" {prefix}" pos = data.find(search) while pos != -1: # skip the space character in the search string pos += 1 # assume symbols are suffixed by either a space # character or a (unix-style) line return spacePos = data.find(" ", pos) lineReturnPos = data.find("\n", pos) if lineReturnPos == -1: endPos = spacePos elif spacePos == -1: endPos = lineReturnPos else: endPos = min(spacePos, lineReturnPos) if endPos == -1: match = data[pos:] pos = -1 else: match = data[pos:endPos] pos = data.find(search, endPos) completes.append(match) return completes
def complete_symbol( prefix: str, parsed_args: argparse.Namespace, **kwargs: object ) -> List[str]: if not prefix or prefix.startswith("-"): # skip reading the map file, which would # result in a lot of useless completions return [] config: Dict[str, Any] = {} diff_settings.apply(config, parsed_args) # type: ignore mapfile = config.get("mapfile") if not mapfile: return [] completes = [] with open(mapfile) as f: data = f.read() # assume symbols are prefixed by a space character search = f" {prefix}" pos = data.find(search) while pos != -1: # skip the space character in the search string pos += 1 # assume symbols are suffixed by either a space # character or a (unix-style) line return spacePos = data.find(" ", pos) lineReturnPos = data.find("\n", pos) if lineReturnPos == -1: endPos = spacePos elif spacePos == -1: endPos = lineReturnPos else: endPos = min(spacePos, lineReturnPos) if endPos == -1: match = data[pos:] pos = -1 else: match = data[pos:endPos] pos = data.find(search, endPos) completes.append(match) return completes
"--max-lines", dest="max_lines", type=int, default=1024, help="The maximum length of the diff, in lines.", ) # Project-specific flags, e.g. different versions/make arguments. if hasattr(diff_settings, "add_custom_arguments"): diff_settings.add_custom_arguments(parser) # type: ignore args = parser.parse_args() # Set imgs, map file and make flags in a project-specific manner. config: Dict[str, Any] = {} diff_settings.apply(config, args) arch = config.get("arch", "mips") baseimg = config.get("baseimg", None) myimg = config.get("myimg", None) mapfile = config.get("mapfile", None) makeflags = config.get("makeflags", []) source_directories = config.get("source_directories", None) objdump_executable = config.get("objdump_executable", None) MAX_FUNCTION_SIZE_LINES = args.max_lines MAX_FUNCTION_SIZE_BYTES = MAX_FUNCTION_SIZE_LINES * 4 COLOR_ROTATION = [ Fore.MAGENTA, Fore.CYAN,
#!/usr/bin/env python3 import capstone as cs from elftools.elf.elffile import ELFFile import diff_settings from pathlib import Path import sys from typing import Any, Dict, Set import utils config: Dict[str, Any] = {} diff_settings.apply(config, {}) base_elf = ELFFile( (Path(__file__).parent.parent / config["baseimg"]).open("rb")) my_elf = ELFFile((Path(__file__).parent.parent / config["myimg"]).open("rb")) my_symtab = my_elf.get_section_by_name(".symtab") if not my_symtab: utils.fail(f'{config["myimg"]} has no symbol table') def get_file_offset(elf, addr: int) -> int: for seg in elf.iter_segments(): if seg.header["p_type"] != "PT_LOAD": continue if seg["p_vaddr"] <= addr < seg["p_vaddr"] + seg["p_filesz"]: return addr - seg["p_vaddr"] + seg["p_offset"] assert False def get_symbol_file_offset(elf, table, name: str) -> int:
) try: from colorama import Fore, Style, Back # type: ignore import ansiwrap # type: ignore import watchdog # type: ignore except ModuleNotFoundError as e: fail(MISSING_PREREQUISITES.format(e.name)) # ==== CONFIG ==== args = parser.parse_args() # Set imgs, map file and make flags in a project-specific manner. config: Dict[str, Any] = {} diff_settings.apply(config, args) # type: ignore arch: str = config.get("arch", "mips") baseimg: Optional[str] = config.get("baseimg") myimg: Optional[str] = config.get("myimg") mapfile: Optional[str] = config.get("mapfile") makeflags: List[str] = config.get("makeflags", []) source_directories: Optional[List[str]] = config.get("source_directories") objdump_executable: Optional[str] = config.get("objdump_executable") MAX_FUNCTION_SIZE_LINES: int = args.max_lines MAX_FUNCTION_SIZE_BYTES: int = MAX_FUNCTION_SIZE_LINES * 4 COLOR_ROTATION: List[str] = [ Fore.MAGENTA, Fore.CYAN,