Skip to content

Commit

Permalink
Made elf.py more resilient
Browse files Browse the repository at this point in the history
* try/except for section generator. lief-project/LIEF#1031
* skip symbol name if symbol not present for relocation. This can happen
  for relative relocations FWII
  • Loading branch information
fzakaria committed Feb 29, 2024
1 parent 4c40044 commit 85efdff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
15 changes: 14 additions & 1 deletion sqlelf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from sqlelf import elf
from sqlelf import sql as api_sql

import logging


@dataclass
class ProgramArguments:
Expand Down Expand Up @@ -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]
),
Expand All @@ -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
)
Expand Down
35 changes: 25 additions & 10 deletions sqlelf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 85efdff

Please sign in to comment.