Last active
August 24, 2024 18:25
-
-
Save obfusk/b2f501023b56a9e6de6a0bb5e2a4dd37 to your computer and use it in GitHub Desktop.
Get ELF build-id w/ pyelftools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# encoding: utf-8 | |
# SPDX-FileCopyrightText: 2024 FC (Fay) Stegerman <[email protected]> | |
# SPDX-License-Identifier: GPL-3.0-or-later | |
from typing import Optional | |
from elftools.elf.elffile import ELFFile | |
from elftools.elf.sections import NoteSection | |
class Error(Exception): | |
pass | |
def elf_build_id(elffile: str) -> Optional[str]: | |
with open(elffile, "rb") as fh: | |
for section in ELFFile(fh).iter_sections(): # type: ignore[no-untyped-call] | |
if isinstance(section, NoteSection): | |
for note in section.iter_notes(): # type: ignore[no-untyped-call] | |
if note["n_type"] == "NT_GNU_BUILD_ID": | |
return note["n_desc"] # type: ignore[no-any-return] | |
return None | |
def main() -> None: | |
import argparse | |
parser = argparse.ArgumentParser(prog="elf-build-id.py", description="Get ELF build-id.") | |
parser.add_argument("elffiles", metavar="ELF", nargs="+") | |
args = parser.parse_args() | |
for elffile in args.elffiles: | |
if build_id := elf_build_id(elffile): | |
print(f"{elffile!r}:\n 0x{build_id}") | |
else: | |
print(f"{elffile!r}:\n Could not find build-id.") | |
if __name__ == "__main__": | |
main() | |
# vim: set tw=80 sw=4 sts=4 et fdm=marker : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment