Skip to content

Instantly share code, notes, and snippets.

@jarkkojs
Created October 30, 2020 11:53
Show Gist options
  • Save jarkkojs/b15c473fe1562354d711206e11dea5a9 to your computer and use it in GitHub Desktop.
Save jarkkojs/b15c473fe1562354d711206e11dea5a9 to your computer and use it in GitHub Desktop.
import sys
from elftools.elf.elffile import ELFFile
PAGE_SIZE = 0x1000
if __name__ == '__main__':
flags2str = ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']
if len(sys.argv) != 2:
sys.exit(1)
with open(sys.argv[1], 'rb') as file:
file = ELFFile(file)
print('{:<5} {:<18} {:<18}'.format('Flags', 'Start', 'End'))
for seg in file.iter_segments():
if seg['p_type'] != 'PT_LOAD':
continue
flags = flags2str[seg['p_flags']]
start = seg['p_offset'] & ~(PAGE_SIZE - 1)
end = start + (seg['p_filesz'] + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)
print('{:<5} 0x{:0>16x} 0x{:0>16x}'.format(flags, start, end))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment