#!/usr/bin/python # coding: utf-8 # Test case: # 1. Python 2.7.6 and 3.7.1 test pass on ubuntu. # 2. Python 2.7.1 test pass on windows. import os,fnmatch import struct import mmap import sys import gzip import zlib import platform from ctypes import * # for generate_elfhdr # typedef struct elf32_hdr{ # unsigned char e_ident[EI_NIDENT]; # Elf32_Half e_type; # Elf32_Half e_machine; # Elf32_Word e_version; # Elf32_Addr e_entry; /* Entry point */ # Elf32_Off e_phoff; # Elf32_Off e_shoff; # Elf32_Word e_flags; # Elf32_Half e_ehsize; # Elf32_Half e_phentsize; # Elf32_Half e_phnum; # Elf32_Half e_shentsize; # Elf32_Half e_shnum; # Elf32_Half e_shstrndx; # } Elf32_Ehdr; # typedef struct elf64_hdr{ # unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ # Elf64_Half e_type; # Elf64_Half e_machine; # Elf64_Word e_version; # Elf64_Addr e_entry; /* Entry point virtual address */ # Elf64_Off e_phoff; /* Program header table file offset */ # Elf64_Off e_shoff; /* Section header table file offset */ # Elf64_Word e_flags; # Elf64_Half e_ehsize; # Elf64_Half e_phentsize; # Elf64_Half e_phnum; # Elf64_Half e_shentsize; # Elf64_Half e_shnum; # Elf64_Half e_shstrndx; # } Elf64_Ehdr; # typedef struct elf32_phdr{ # Elf32_Word p_type; # Elf32_Off p_offset; # Elf32_Addr p_vaddr; # Elf32_Addr p_paddr; # Elf32_Word p_filesz; # Elf32_Word p_memsz; # Elf32_Word p_flags; # Elf32_Word p_align; # } Elf32_Phdr; # typedef struct elf64_phdr { # Elf64_Word p_type; # Elf64_Word p_flags; # Elf64_Off p_offset; /* Segment file offset */ # Elf64_Addr p_vaddr; /* Segment virtual address */ # Elf64_Addr p_paddr; /* Segment physical address */ # Elf64_Xword p_filesz; /* Segment size in file */ # Elf64_Xword p_memsz; /* Segment size in memory */ # Elf64_Xword p_align; /* Segment alignment, file & memory */ # } Elf64_Phdr; # /* 32-bit ELF base types. */ # typedef __u32 Elf32_Addr; # typedef __u16 Elf32_Half; # typedef __u32 Elf32_Off; # typedef __s32 Elf32_Sword; # typedef __u32 Elf32_Word; # /* 64-bit ELF base types. */ # typedef __u64 Elf64_Addr; # typedef __u16 Elf64_Half; # typedef __s16 Elf64_SHalf; # typedef __u64 Elf64_Off; # typedef __s32 Elf64_Sword; # typedef __u32 Elf64_Word; # typedef __u64 Elf64_Xword; # typedef __s64 Elf64_Sxword; ELFMAG = "\177ELF" SELFMAG = 4 ELFCLASS32 = 1 ELFCLASS64 = 2 ELFDATA2MSB = 2 ELFDATA2LSB = 1 EV_CURRENT = 1 ELF_OSABI = 0 EI_CLASS = 4 EI_DATA = 5 EI_VERSION = 6 EI_OSABI = 7 EI_PAD = 8 EI_NIDENT = 16 ET_CORE = 4 EM_ARM64 = 0xb7 EM_ARM = 0x28 ELF_CORE_EFLAGS = 0 PT_LOAD = 1 PT_NOTE = 4 PF_R = 0x4 PF_W = 0x2 PF_X = 0x1 class ELE32_HEADER(LittleEndianStructure): _pack_ = 1 _fields_ = [ ('e_ident0', c_char * 4), ('e_ident1', c_uint8), ('e_ident2', c_uint8), ('e_ident3', c_uint8), ('e_ident4', c_uint8), ('e_ident5', c_uint64), ('e_type', c_uint16), ('e_machine', c_uint16), ('e_version', c_uint32), ('e_entry', c_uint32), ('e_phoff', c_uint32), ('e_shoff', c_uint32), ('e_flags', c_uint32), ('e_ehsize', c_uint16), ('e_phentsize', c_uint16), ('e_phnum', c_uint16), ('e_shentsize', c_uint16), ('e_shnum', c_uint16), ('e_shstrndx', c_uint16), ] def encode(self): return string_at(addressof(self), sizeof(self)) def decode(self, data): memmove(addressof(self), data, sizeof(self)) return len(data) class ELE32_P_HEADER(LittleEndianStructure): _pack_ = 1 _fields_ = [ ('p_type', c_uint32), ('p_offset', c_uint32), ('p_vaddr', c_uint32), ('p_paddr', c_uint32), ('p_filesz', c_uint32), ('p_memsz', c_uint32), ('p_flags', c_uint32), ('p_align', c_uint32), ] def encode(self): return string_at(addressof(self), sizeof(self)) def decode(self, data): memmove(addressof(self), data, sizeof(self)) return len(data) class ELE64_HEADER(LittleEndianStructure): _pack_ = 1 _fields_ = [ ('e_ident0', c_char * 4), ('e_ident1', c_uint8), ('e_ident2', c_uint8), ('e_ident3', c_uint8), ('e_ident4', c_uint8), ('e_ident5', c_uint64), ('e_type', c_uint16), ('e_machine', c_uint16), ('e_version', c_uint32), ('e_entry', c_uint64), ('e_phoff', c_uint64), ('e_shoff', c_uint64), ('e_flags', c_uint32), ('e_ehsize', c_uint16), ('e_phentsize', c_uint16), ('e_phnum', c_uint16), ('e_shentsize', c_uint16), ('e_shnum', c_uint16), ('e_shstrndx', c_uint16), ] def encode(self): return string_at(addressof(self), sizeof(self)) def decode(self, data): memmove(addressof(self), data, sizeof(self)) return len(data) class ELE64_P_HEADER(LittleEndianStructure): _pack_ = 1 _fields_ = [ ('p_type', c_uint32), ('p_flags', c_uint32), ('p_offset', c_uint64), ('p_vaddr', c_uint64), ('p_paddr', c_uint64), ('p_filesz', c_uint64), ('p_memsz', c_uint64), ('p_align', c_uint64), ] def encode(self): return string_at(addressof(self), sizeof(self)) def decode(self, data): memmove(addressof(self), data, sizeof(self)) return len(data) # struct sysdump_mem { # unsigned long paddr; # unsigned long vaddr; # unsigned long soff; # unsigned long size; # unsigned long type; # }; class Minidump_Mem_Arm32(LittleEndianStructure): _pack_ = 1 _fields_ = [ ('paddr', c_uint32), ('vaddr', c_uint32), ('soff', c_uint32), ('size', c_uint32), ('type', c_uint32), ] def encode(self): return string_at(addressof(self), sizeof(self)) def decode(self, data): memmove(addressof(self), data, sizeof(self)) return len(data) class Minidump_Mem_Arm64(LittleEndianStructure): _pack_ = 1 _fields_ = [ ('paddr', c_uint64), ('vaddr', c_uint64), ('soff', c_uint64), ('size', c_uint64), ('type', c_uint64), ] def encode(self): return string_at(addressof(self), sizeof(self)) def decode(self, data): memmove(addressof(self), data, sizeof(self)) return len(data) MINIDUMP_MEM_MAX = 251 DATA_OFFSET = 0x2000 #arm32 ELF32_HEADER_SIZE = 54 ELF32_P_HEADER_SIZE = 32 MINIDUMP_MEM32_SIZE = 20 #arm64 ELF64_HEADER_SIZE = 64 ELF64_P_HEADER_SIZE = 56 MINIDUMP_MEM64_SIZE = 40 #for generate_elfhdr end # Reference kernel/printk/printk.c PRINKT_LOG_STRUCT_FORMAT = '