Files
TheRock/rocm-systems/shared/kpack/python/rocm_kpack/format_detect.py
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

95 lines
2.5 KiB
Python

"""
Binary format detection utilities.
This module provides functions to detect whether a binary file is ELF or PE/COFF
format, enabling the generic API to dispatch to the correct implementation.
"""
from pathlib import Path
# Magic bytes for format detection
ELF_MAGIC = b"\x7fELF"
DOS_MAGIC = b"MZ"
PE_SIGNATURE = b"PE\x00\x00"
class UnsupportedBinaryFormat(ValueError):
"""Raised when a binary is not ELF or PE/COFF format."""
pass
def detect_binary_format(path: Path) -> str:
"""Detect whether a binary is ELF or PE/COFF format.
Args:
path: Path to binary file
Returns:
"elf" for ELF binaries, "coff" for PE/COFF binaries
Raises:
UnsupportedBinaryFormat: If the binary is neither ELF nor PE/COFF
FileNotFoundError: If the file doesn't exist
"""
with open(path, "rb") as f:
# Read enough for both checks
header = f.read(64)
if len(header) < 4:
raise UnsupportedBinaryFormat(f"File too small to be a valid binary: {path}")
# Check for ELF magic
if header[:4] == ELF_MAGIC:
return "elf"
# Check for PE/COFF (DOS header with PE signature)
if header[:2] == DOS_MAGIC:
# Read PE header offset from DOS header at offset 0x3c
if len(header) >= 0x40:
pe_offset = int.from_bytes(header[0x3C:0x40], "little")
# Validate PE offset is reasonable (within first 1MB, aligned)
if pe_offset < 0x40 or pe_offset > 0x100000:
raise UnsupportedBinaryFormat(
f"Invalid PE header offset {pe_offset:#x}: {path}"
)
# Read PE signature
with open(path, "rb") as f:
f.seek(pe_offset)
pe_sig = f.read(4)
if pe_sig == PE_SIGNATURE:
return "coff"
raise UnsupportedBinaryFormat(f"Binary is neither ELF nor PE/COFF format: {path}")
def is_elf_binary(path: Path) -> bool:
"""Check if a binary is ELF format.
Args:
path: Path to binary file
Returns:
True if ELF, False otherwise
"""
try:
return detect_binary_format(path) == "elf"
except (UnsupportedBinaryFormat, FileNotFoundError):
return False
def is_pe_binary(path: Path) -> bool:
"""Check if a binary is PE/COFF format.
Args:
path: Path to binary file
Returns:
True if PE/COFF, False otherwise
"""
try:
return detect_binary_format(path) == "coff"
except (UnsupportedBinaryFormat, FileNotFoundError):
return False