Skip to content

Instantly share code, notes, and snippets.

@bc-lee
Last active August 3, 2024 06:46
Show Gist options
  • Save bc-lee/a0b3e9b44d92db6f9ab6ca5d0784a836 to your computer and use it in GitHub Desktop.
Save bc-lee/a0b3e9b44d92db6f9ab6ca5d0784a836 to your computer and use it in GitHub Desktop.
check_elf_page_size.py
#!/usr/bin/env python3
# Released into the public domain. No attribution required.
__doc__ = """
This script reads an ELF file and prints the page size of the first LOAD segment.
"""
import os
import sys
# third_party
# to install, run `pip install pyelftools`
from elftools.elf.elffile import ELFFile
def get_page_size(elf_file_path):
with open(elf_file_path, 'rb') as file:
try:
elf = ELFFile(file)
for segment in elf.iter_segments():
if segment.header.p_type == 'PT_LOAD':
print(f"Page Size of {elf_file_path}: {segment.header.p_align}")
break
except ELFError as e:
print(f"Failed to parse file: {elf_file_path}: {e}")
def main(argv):
if len(argv) < 2:
print(__doc__)
print(f"Usage: {argv[0]} <path_to_elf_file> [<path_to_elf_file> ...]")
return 1
else:
for elf_file in argv[1:]:
if os.path.exists(elf_file):
get_page_size(elf_file)
else:
print(f"File not found: {elf_file}")
sys.exit(1)
if __name__ == "__main__":
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment