import base64 import contextlib import struct import tempfile import unittest from drgn.corereader import CoreReader from drgn.dwarf import ElfFormatError @contextlib.contextmanager def tmpfile(data): file = tempfile.NamedTemporaryFile() try: file.write(data) file.flush() yield file finally: file.close() def make_elf_file(segments=None): if segments is None: segments = [(0xffff0000, b'foobar\0\0')] buf = bytearray(64 + 56 * len(segments)) struct.pack_into( '<16sHHLQQQLHHHHHH', buf, 0, b'\x7fELF\x02\x01\x01\0\0\0\0\0\0\0\0\0', # e_ident 4, # e_type (ET_CORE) 62, # e_machine (EM_X86_64) 1, # e_version (EV_CURRENT) 0, # e_entry 64, # e_phoff (right after the header) 0, # e_shoff 0, # e_flags 64, # e_ehsize 56, # e_phentsize len(segments), # e_phnum 0, # e_shentsize 0, # e_shnum 0, # e_shstrndx ) for i, segment in enumerate(segments): if len(buf) % 4096: buf.extend(bytes(4096 - len(buf) % 4096)) struct.pack_into( '