diff --git a/sqlelf/cli.py b/sqlelf/cli.py index 2c2e8ca..fff6ccd 100644 --- a/sqlelf/cli.py +++ b/sqlelf/cli.py @@ -9,6 +9,8 @@ from sqlelf import elf from sqlelf import sql as api_sql +import logging + @dataclass class ProgramArguments: @@ -71,11 +73,16 @@ def start(args: list[str] = sys.argv[1:], stdin: TextIO = sys.stdin) -> None: ) # Iterate through our arguments and if one of them is a directory explode it out + # Assuming program_args.filenames is a list of files and directories filenames: list[str] = reduce( lambda a, b: a + b, map( lambda dir: ( - [os.path.join(dir, f) for f in os.listdir(dir)] + [ + os.path.join(root, file) + for root, _, files in os.walk(dir) + for file in files + ] if os.path.isdir(dir) else [dir] ), @@ -89,6 +96,12 @@ def start(args: list[str] = sys.argv[1:], stdin: TextIO = sys.stdin) -> None: if len(filenames) == 0: sys.exit("No valid ELF files were provided") + # Setup the logging config + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s", + ) + sql_engine = api_sql.make_sql_engine( filenames, recursive=program_args.recursive, cache_flags=program_args.cache_flag ) diff --git a/sqlelf/elf.py b/sqlelf/elf.py index 1549a02..f23ea5e 100644 --- a/sqlelf/elf.py +++ b/sqlelf/elf.py @@ -21,6 +21,10 @@ from sqlelf._vendor.elftools.dwarf.die import DIE as DIE_t from sqlelf._vendor.elftools.elf.elffile import ELFFile +import logging + +LOG = logging.getLogger(__name__) + @dataclass class Generator: @@ -244,14 +248,23 @@ def sections_generator() -> Iterator[dict[str, Any]]: # as they can be costly binary_name = binary.path for section in binary.sections: - yield { - "path": binary_name, - "name": section.name, - "offset": section.offset, - "size": section.size, - "type": section.type.__name__, - "content": bytes(section.content), - } + try: + yield { + "path": binary_name, + "name": section.name, + "offset": section.offset, + "size": section.size, + "type": section.type.__name__, + "content": bytes(section.content), + } + except RuntimeError: + # TODO(fzakaria): LIEF is failing to parse some section types: + # https://github.com/lief-project/LIEF/issues/1031 + # Just skip them for now + LOG.warning( + "Failed to parse section: %s (%s)", section.name, binary_name + ) + pass generator = Generator.make_generator( ["path", "name", "offset", "size", "type", "content"], @@ -443,8 +456,10 @@ def relocations_generator() -> Iterator[dict[str, Any]]: # https://refspecs.linuxbase.org/elf/gabi4+/ch4.reloc.html "is_rela": relocation.is_rela, "purpose": relocation.purpose.__name__, - "section": relocation.section.name if relocation.section else None, - "symbol": relocation.symbol.name, + "section": ( + relocation.section.name if relocation.section else None + ), + "symbol": relocation.symbol.name if relocation.symbol else None, "symbol_table": ( relocation.symbol_table.name if relocation.symbol_table